计蒜客 蒜头图并查集

    xiaoxiao2023-10-10  164

    #include <iostream> #include <cmath> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int f[1000]; int find(int x) { return f[x]== x? x: f[x]=find(f[x]); } void join(int x,int y) { int aa=find(x); int bb=find(y); if(aa!=bb) f[aa]=bb; } int main(int argc, char** argv) { int i; for(i=0;i<1000;i++) f[i]=i; int n,m; cin>>n>>m; long long ans=1; for(i=0;i<m;i++) { int a,b; cin>>a>>b; int finda=find(a); int findb=find(b); if(finda==findb) { ans++; } else { join(a,b); } cout<<pow(2,ans-1)-1<<endl; } return 0; }
    最新回复(0)