• 并查集


    基础:

    1102     难度1

    1213     难度1

    1232     难度1

    1233     难度1

    稍加思想:

    1198      打表很恶心,难度 1.5

    1269     (有向图是否联通) Tarjan 难度1.5     并查集 难度2

    1272     (无向图是否是棵树)  难度2

    1325     (有向图是否是棵树)转化思想 难度2

    1598      排序乱搞 难度2

    2094

    2419 

    2676

    2860  

    3038 

    317X

    3635

     3938 

    4496

    5253 

    5441

    5723

    5861

    5631

    HDU1102
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<memory.h>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int n,q,Map[110][110];
    int a[1000000],b[1000000];
    int fa[110],L,R;
    int Min,ans;
    int _fa(int v){
    	if(fa[v]==v) return v;
    	fa[v]=_fa(fa[v]);
    	return fa[v];
    }
    void _merge(int u,int v){
    	fa[u]=v;
    }
    int main()
    {
    	int cnt,i,j,k,x,y,faa,fab;
    	while(~scanf("%d",&n)){
    		ans=0;
    		cnt=n-1;
    		for(i=1;i<=n;i++) fa[i]=i;
    		for(i=1;i<=n;i++)
    		 for(j=1;j<=n;j++)
    		  scanf("%d",&Map[i][j]);
    		  
    		scanf("%d",&q);
    		for(i=1;i<=q;i++)
    		{
    			scanf("%d%d",&x,&y);
    			faa=_fa(x);
    			fab=_fa(y);
    			if(faa!=fab) {
    			   cnt--;
    			   _merge(faa,fab);
    			}
    		}
    		
    		for(k=1;k<=cnt;k++){
    			Min=10000000;
    			for(i=1;i<=n;i++)
    			 for(j=i+1;j<=n;j++){
    					faa=_fa(i);
    					fab=_fa(j);
    					if(faa!=fab&&Min>Map[i][j]) {
    						Min=Map[i][j];
    						L=i;
    						R=j;
    					}
    			 }
    			 ans+=Map[L][R];
    			 _merge(_fa(L),_fa(R));
    		}
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    hdu1198
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<memory.h>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int fa[10000],L,R;
    int x[5]={0,1,-1,0,0};
    int y[5]={0,0,0,1,-1};
    char c[100][100]; 
    int Min,ans;
    int _fa(int v){
    	if(fa[v]==v) return v;
    	fa[v]=_fa(fa[v]);
    	return fa[v];
    }
    void _merge(int u,int v){
    	fa[u]=v;
    }
    bool _match(char x,char y,int k){
    	if(k==1){
    		if((x=='C'||x=='D'||x=='E'||x=='H'||x=='I'||x=='J'||x=='K')&&(y=='A'||y=='B'||y=='E'||y=='G'||y=='H'||y=='J'||y=='K')) return true;
            else return false;
    	}
    	else if(k==2){
    		if((y=='C'||y=='D'||y=='E'||y=='H'||y=='I'||y=='J'||y=='K')&&(x=='A'||x=='B'||x=='E'||x=='H'||x=='G'||x=='J'||x=='K')) return true;
            else return false;
    	}
    	else if(k==3){
    		if((x=='B'||x=='D'||x=='F'||x=='G'||x=='I'||x=='J'||x=='K')&&(y=='A'||y=='C'||y=='F'||y=='G'||y=='H'||y=='I'||y=='K')) return true;
            else return false;
    	}
    	else if(k==4){
    		if((y=='D'||x=='B'||y=='F'||y=='G'||y=='I'||y=='J'||y=='K')&&(x=='A'||x=='C'||x=='F'||x=='G'||x=='H'||x=='I'||x=='K')) return true;
            else return false;
    	}
    	return false;
    }
    int main()
    {
    	int n,m,i,j,k,faa,fab;
    	while(~scanf("%d%d",&n,&m)){
    		if(m<=0||n<=0) return 0;
    		ans=n*m;
    	    for(i=1;i<=n;i++)
    	     for(j=1;j<=m;j++)
    	      {
    				fa[(i-1)*m+j]=(i-1)*m+j;
    				cin>>c[i][j];
    		  }
    		for(i=1;i<=n;i++)
    		  for(j=1;j<=m;j++){
    			for(k=1;k<=4;k++){
    				if(i+x[k]>=1&&i+x[k]<=n&&j+y[k]>=1&&j+y[k]<=m){
    					faa=_fa((i-1)*m+j);
    				    fab=_fa((i+x[k]-1)*m+j+y[k]);
    				    if(faa!=fab&&_match(c[i][j],c[i+x[k]][j+y[k]],k)){
    					  ans--;
    				      _merge(faa,fab);
    				    }
    				}
    			}	
    		}
    		cout<<ans<<endl;	
    	}
    	return 0;
    }


    HDU1213
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<memory.h>
    #include<algorithm>
    using namespace std;
    int fa[1010];
    int _fa(int v){
    	if(v==fa[v]) return v;
    	fa[v]=_fa(fa[v]);
    	return fa[v];
    }
    void _merge(int u,int v){
    	fa[u]=v;
    }
    int main()
    {
    	int T,n,m,i;
    	int faa,fab,a,b;
    	cin>>T;
    	while(T--){
    		memset(fa,0,sizeof(fa));
    		 cin>>n>>m;
    		 for(i=1;i<=m;i++){
    				cin>>a>>b;
    				if(fa[a]==0) fa[a]=a;
    				if(fa[b]==0) fa[b]=b;
    				faa=_fa(a);
    				fab=_fa(b);
    				if(fab!=faa) {
    					n--;
    					_merge(faa,fab);
    				}
    		}
    		cout<<n<<endl;
    	}
    	return 0;
    }
    HDU1232
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<map>
    #include<algorithm>
    #include<memory.h> 
    using namespace std;
    int fa[10010];
    int _fa(int v)
    {
        if(v==fa[v]) return v;
        fa[v]=_fa(fa[v]);
        return fa[v];
    }
    void _merge(int u,int v){
        fa[u]=v;
    }
    int main()
    {
        int i,j,k,n,m,ans,x,y,cnt;
        int faa,fab;
        while(~scanf("%d",&n)){
            if(n<=0) return 0;
            ans=n-1;
            memset(fa,0,sizeof(fa));
            cin>>m;
            for(i=1;i<=m;i++){
                cin>>x>>y;
                if(fa[x]==0) fa[x]=x;
                if(fa[y]==0) fa[y]=y;
                faa=_fa(x);
                fab=_fa(y);
                if(faa!=fab) {
                    ans--;
                    _merge(faa,fab);
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }

    HDU1233
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<map>
    #include<algorithm>
    #include<memory.h> 
    using namespace std;
    int fa[110];
    struct in
    {
    	int a,b,c;
    }s[10000];
    bool cmp(in x,in y){
    	return x.c<y.c;
    }
    int _fa(int v)
    {
    	if(v==fa[v]) return v;
    	fa[v]=_fa(fa[v]);
    	return fa[v];
    }
    void _merge(int u,int v){
    	fa[u]=v;
    }
    int main()
    {
    	int i,j,k,n,m,ans,tmp,cnt;
    	while(~scanf("%d",&n)){
    		if(n<=0) return 0;
    		ans=0;cnt=0;
    		tmp=n*(n-1)/2;
    		for(i=1;i<=tmp;i++){
    		    scanf("%d%d%d",&s[i].a,&s[i].b,&s[i].c);
    		}
    		sort(s+1,s+tmp+1,cmp);
    		for(i=1;i<=n;i++) fa[i]=i;
    		for(i=1;i<=tmp&&cnt<n;i++){
    			int faa=_fa(s[i].a);
    			int fab=_fa(s[i].b);
    			if(faa!=fab){
    				cnt++;
    				ans+=s[i].c;
    				_merge(faa,fab);
    			}
    		}
    		cout<<ans<<endl;
    	}
    	return 0;
    }
    转HDU1269
    #include<stdio.h>
    #include<string.h>
    int pre[2][100010];
    int n,m;
    int find(int a,int i)
    {
    	int r=a;
    	while(r!=pre[i][r])
    	{
    		r=pre[i][r];
    	}
    	return r;
    }
    void fun(int a,int b)
    {	
    	if(a!=n)
    	{
    		int fa=find(a,0),fb=find(b,0);
    		if(fa!=fb)
    			pre[0][a]=b;
    	}
    	if(b!=n)
    	{
    		int fa=find(a,1),fb=find(b,1);
    		if(fa!=fb)
    			pre[1][b]=a;
    	}
    }
    int main()
    {
    	while(scanf("%d%d",&n,&m)!=EOF,n||m)
    	{
    		int i,w=1;
    		for(i=0;i<=n;i++)
    		{
    			pre[0][i]=pre[1][i]=i;
    		}
    		while(m--)
    		{
    			int a,b;
    			scanf("%d%d",&a,&b);
    			fun(a,b);
    		}
    		for(i=1;i<=n;i++)
    		{
    			if(find(i,0)!=n||find(i,1)!=n)
    			{
    				w=0;
    				break;
    			}
    		}
    		if(w)
    			printf("Yes
    ");
    		else
    			printf("No
    ");
    	}
    }
    
    
    
    
    
    
    HDU1272
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<map>
    #include<algorithm>
    #include<memory.h> 
    using namespace std;
    int fa[100010],w[100010];
    int s[100010],cnt;
    int _fa(int v)
    {
    	if(v==fa[v]) return v;
    	fa[v]=_fa(fa[v]);
    	return fa[v];
    }
    void _merge(int u,int v){
    	if(w[u]>w[v]) {
    		int tmp=u;u=v;v=tmp;
    	}
    	w[v]+=w[u];
    	fa[u]=v;
    }
    int main()
    {
    	int a,b;
    	int faa,fab; 
    	bool flag=true;
    	while(~scanf("%d%d",&a,&b)){
    		if(a==-1&&b==-1) return 0;
    		else if(a==0&&b==0) {
    			faa=_fa(s[1]);//判断是否联通 
    			for(int i=2;i<=cnt;i++){
    				fab=_fa(s[i]);
    				if(faa!=fab){
    					flag=false;
    					break;
    				}
    			}
    		    if(flag) printf("Yes
    ");
    		    else printf("No
    ");
    			flag=true;//更新 
    			memset(fa,0,sizeof(fa));
    			memset(w,0,sizeof(w));
    			cnt=0;
    		}
    		else {
    			if(fa[a]==0) {fa[a]=a;w[a]=1;s[++cnt]=a;}//离散化 
    			if(fa[b]==0) {fa[b]=b;w[b]=1;s[++cnt]=b;}
    			faa=_fa(a);
    		    fab=_fa(b);
    			if(faa==fab) flag=false;
    			else _merge(faa,fab);
    		}
    	}
    	return 0;
    }


    HDU1325
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<map>
    #include<algorithm>
    #include<memory.h> 
    using namespace std;
    int fa[100010],ru[100010];
    int s[100010],cnt,num;
    int _fa(int v)
    {
        if(v==fa[v]) return v;
        fa[v]=_fa(fa[v]);
        return fa[v];
    }
    int main()
    {
        int a,b;
        int faa,fab; 
        int Case=0;
        bool flag=true;
        while(1){
            flag=true;//更新 
            memset(fa,0,sizeof(fa));
            memset(ru,0,sizeof(ru));
            cnt=0;
            while(scanf("%d%d",&a,&b)&&a&&b){
                if(a<0||b<0) return 0;
                 if(fa[a]==0) {fa[a]=a;s[++cnt]=a;}//离散化 
                 if(fa[b]==0) {fa[b]=b;s[++cnt]=b;}
                 faa=_fa(a);
                 fab=_fa(b);
                 ru[b]++;
                 if(ru[b]>1)  flag=false;
                 if(faa==fab) flag=false;
                 fa[fab]=faa;
               }            
                faa=_fa(s[1]);//判断是否联通 
                for(int i=2;i<=cnt;i++){
                    fab=_fa(s[i]);
                    if(faa!=fab){
                        flag=false;
                        break;
                    }
                }
                if(flag) printf("Case %d is a tree.
    ",++Case);
                else printf("Case %d is not a tree.
    ",++Case);
        }
        return 0;
    }
    
    
    
     


    It is your time to fight!
  • 相关阅读:
    mysql中的内连接,外连接
    MySQL左连接、右连接
    attempted to assign id from null onetoone
    Hibernate session方法
    java.sql.SQLException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error
    hibernate多对一、一对一、一对多、多对多的配置方法
    齐头并进
    w5100的板子做回来了,再次犯错误。
    一个阶段的任务基本完成了
    任务繁重
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7603919.html
Copyright © 2020-2023  润新知