其他分享
首页 > 其他分享> > 1385:团伙(group)

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;
}

标签:group,ene,int,back,vis,push,1385,include,团伙
来源: https://www.cnblogs.com/sxrekord/p/gang.html