当前位置:网站首页>Program design TIANTI race l2-007 family real estate (it's too against the sky. I always look at the solution of the problem, resulting in forgetting the problem and looking up how to write the collec

Program design TIANTI race l2-007 family real estate (it's too against the sky. I always look at the solution of the problem, resulting in forgetting the problem and looking up how to write the collec

2022-04-21 16:39:00 Find a derivative first

subject
The question : A little . Classical sorting problem .
Ideas : In a very stupid way , Drawing dfs Merge with union search set , Finally sort the output .
Time complexity : O( Yes )
Code :

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
const int M = 9999;
int n,m,k,T;
struct node{
    
	int id = 77777; // Number 
    int num = 1; // The number of 
    double tot; // The number of properties 
    double av; // Property area 
    bool operator<(const node&rhs)
    {
    
    	if(av != rhs.av) return av > rhs.av;
    	return id < rhs.id;
    }
}a[N];
vector<int> va[N];
int fa[N];
bool vis[N];
int find(int x)
{
    
	return x==fa[x]?x:fa[x] = find(fa[x]);
}
bool Merge(int u,int v)
{
    
	u = find(u),v = find(v);
	if(u != v)
	{
    
		fa[v] = u;
		return 1;
	}
	return 0;
}
void dfs(int st,int i)
{
    
	for(int j:va[i])
	{
    
		// if(i==1) cout<<j<<"?\n"; 
		if(Merge(st,j))
		{
    
			// a[st].id = min(a[i].id,j);
			a[st].num += a[j].num;
			a[st].tot += a[j].tot;
			a[st].av += a[j].av;
			dfs(st,j);
		}
	}
}
void solve()
{
    
	for(int i=0;i<=M;++i) fa[i] = i;
	cin>>n;
	for(int i=0;i<n;++i)
	{
    
		int x,y,z;int num;
		cin>>x>>y>>z>>num;
		vis[x]=1;
		if(y!=-1) va[x].push_back(y),va[y].push_back(x),vis[y]=1;
		if(z!=-1) va[x].push_back(z),va[z].push_back(x),vis[z]=1;
		while(num--)
		{
    
			int t; cin>>t;
			vis[t] = 1;
			va[x].push_back(t);
			va[t].push_back(x);
		}
		cin>>a[x].tot>>a[x].av;
		a[x].id = x;
	}
	for(int i=0;i<=9999;++i)
	{
    
		if(vis[i]&&find(i)==i)
		{
    
			a[i].id = i;
			dfs(i,i);
		}
	}
	vector<node> va;
	for(int i=0;i<=M;++i)
	{
    
		if(vis[i] && find(i)==i)
		{
    
			a[i].av /= a[i].num;
		    a[i].tot /= a[i].num;
		    va.push_back(a[i]);
		}
	}
	sort(va.begin(),va.end());
	cout<<va.size()<<"\n";
	for(int i=0;i<va.size();++i)
	{
    
		printf("%04d ",va[i].id);
		cout<<va[i].num;
		printf(" %.3lf %.3lf\n",va[i].tot,va[i].av);
	}
}
signed main(void)
{
    
	solve();
	return 0;
}

版权声明
本文为[Find a derivative first]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211632354596.html