[NWERC 2019] F. Firetrucks Are Red 并差集

并查集问题,认为难点在于建图以及用map去标记。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>	
#include<unordered_set>
#include<unordered_map>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define lowbit(x) x&-x

const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=4e5+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int f[maxn];
int find(int x) { return x==f[x]?x:f[x]=find(f[x]); }
unordered_map<int,int> book;
struct Node
{
	int p,num;
	Node(int p,int num):p(p),num(num) {}
};
vector<Node> g[maxn];
void dfs(int u,int f)
{
	for(int i=0;i<g[u].size();i++)
	{
		Node v=g[u][i];
		if(v.p==f) continue;
		printf("%d %d %d\n",u,v.p,v.num);
		dfs(v.p,u);
	}
}

int main()
{
	int n,m;
	scanf("%d",&n);
	book.clear();
	for(int i=1;i<=n;i++) f[i]=i,g[i].clear();
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&m);
		for(int j=1;j<=m;j++)
		{
			int x; scanf("%d",&x);
			if(book.count(x))
			{
				int v=book[x];
				if(find(v)==find(i)) continue;
				f[find(i)]=find(v);
				g[i].push_back(Node(v,x));
				g[v].push_back(Node(i,x));
			}
			else book[x]=i;
		}
	}
	for(int i=2;i<=n;i++)
	{
		if(find(i)!=find(1))
		{
			printf("impossible");
			return 0;
		}
	}
	dfs(1,0);
}
上一篇:【填坑2】获取设备所有网卡的mac地址


下一篇:Android开发如何去除标题栏title