Personnel grouping problem

Personnel packet, a total of n list of individuals, input everyone I know, the list if a list there b or b is there a, then a and b know each other, if a recognized b, b understanding C, through the introduction , a will recognize c, you put all personnel groups, each group of people are able to recognize each other, and the minimum number of packets.

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
void dfs(vector<vector<int>>&ma,int *visited,int *visited2,int pos,bool&flag){
    visited[pos]=1;
    for(int j=0;j<ma[pos].size();j++){
        if(visited[ma[pos][j]-1])
        {
            if(visited2[ma[pos][j]-1])
               flag=false;
            continue;
        }
        else{
            dfs(ma,visited,visited2,ma[pos][j]-1,flag);
        }
    }
}

int seperate(vector<vector<int>>&ma){ 
    int res=0;
    int visited[ma.size()]={};
    for(int i=0;i<ma.size();i++){
        if(visited[i])
            continue;
        bool flag=true;
        int visited2[ma.size()];
        for(int j=0;j<ma.size();j++)
             visited2[j]=visited[j];
             
        dfs(ma,visited,visited2,i,flag);
        if(flag)
          res =res +1;
    }
    return res;
}

int main ()
{
int n;
cin>>n;
vector<vector<int>>ma(n);
for(int i=0;i<n;i++){
    int t;
    cin>>t;
    while(t){
        ma[t-1].push_back(i+1);
        cin>>t;
    }
}
cout<<seperate(ma);
return 0;
}

Guess you like

Origin www.cnblogs.com/qiuhaifeng/p/11490510.html