1385:团伙(group)

团伙

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int N=1005;

vector<int> fri[N];
vector<int> ene[N];
bool vis[N];
queue<int> q;

int solve(int n){
	memset(vis,0,sizeof(vis));
	int ans=0;
	for(int i=1;i<=n;i++){
		if(vis[i])continue;
		ans++;
		vis[i]=1;
		q.push(i);
		while(!q.empty()){
			int u=q.front();
			q.pop();
			for(int j=0;j<fri[u].size();j++){
				if(!vis[fri[u][j]]){
					vis[fri[u][j]]=1;
					q.push(fri[u][j]);
				}
			}
			for(int j=0;j<ene[u].size();j++){
				int t=ene[u][j];
				for(int k=0;k<ene[t].size();k++){
					if(!vis[ene[t][k]]){
						vis[ene[t][k]]=1;
						q.push(ene[t][k]);
					}					
				}

			}
		}
	}
	return ans;
}
int main(){
    int n,m;
	cin>>n>>m;
	while(m--){
		int p,a,b;
		scanf("%d%d%d",&p,&a,&b);
		if(p){
			ene[a].push_back(b);
			ene[b].push_back(a);
		}else{
			fri[a].push_back(b);
			fri[b].push_back(a);
		}
	}
	printf("%d",solve(n));
    return 0;
}
posted @ 2021-10-30 18:54  Rekord  阅读(302)  评论(0编辑  收藏  举报