zoukankan      html  css  js  c++  java
  • poj1308 Is is a tree?

    题目链接:https://vjudge.net/problem/POJ-1308

    题意:给定一些有向边,判断能否形成一棵树

    其实有向边就可以看成无向边,然后就是判环和判连通的问题了,可以用并查集。输入数据不一定是从1到n,所以加一个简单的离散化,这个在noi2015d1t1也用到了。其他也没什么注意点

    #include<cstdio>
    #include<set>
    using namespace std;
    
    const int maxn=100000+10;
    int n,i,x,y,f,num,c;
    int par[maxn],a[maxn];
    
    int find(int x){return par[x]==x?x:par[x]=find(par[x]);}
    
    int main(){
    	scanf("%d%d",&x,&y);
    	c=0; set<int> st;
    	while (x!=-1&&y!=-1){
    	  num=0; st.clear(); c++; f=0;
    	  for (i=1;i<=maxn;i++) {
    	  	par[i]=i;a[i]=0;
    	  }
    	  while (x>0&&y>0){
    	  	if (a[x]==0) a[x]=(++num); if (a[y]==0) a[y]=(++num);
    	  	int xx=find(par[a[x]]); int yy=find(par[a[y]]);
    	  	if (xx==yy) f=1; else par[xx]=yy;
    	  	scanf("%d%d",&x,&y);
    	  }
    	  for (i=1;i<=num;i++) par[i]=find(par[i]);
    	  for (i=1;i<=num;i++) st.insert(par[i]);
    	  if (st.size()>1) f=1;
    	  if (f==0) printf("Case %d is a tree.
    ",c);
    	  else printf("Case %d is not a tree.
    ",c);
    	  scanf("%d%d",&x,&y);
    	}
    	return 0;
    }
  • 相关阅读:
    jqGrid表格控件的学习
    list 集合筛选数据
    MySQL跨域
    11-Index页面
    11-Comment页面
    11-Add页面
    11-UploadFile
    11-控制器UI
    11-控制器
    11-数据访问层
  • 原文地址:https://www.cnblogs.com/edmunds/p/13430758.html
Copyright © 2011-2022 走看看