SPOJ Problem 1436:Is it a tree

给出一个图,求它是不是树。。

首先,一个图如果是树那么边数就是点数-1,然后再判断所有点是否连通。这里可以用并查集搞一下。

代码如下:

#include<cstdio>
#include<cstring>
int f[10005],x,y,i,j,n,m;
int getf(int s){return f[s]==s?s:f[s]=getf(f[s]);}
void exch(int x,int y){int t=y;y=x;x=t;}
int main(){
    scanf("%d%d",&n,&m);
    if (m!=n-1){printf("NO\n");return 0;}
    for (i=1;i<=n;i++)
        f[i]=i;
    for (i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
        if (x>y)exch(x,y);
        if (getf(x)!=getf(y))f[y]=getf(x);
    }
    for (i=1;i<=n;i++)
        if(getf(i)!=1){printf("NO\n");return 0;}
    printf("YES\n");
}
时间: 2024-11-15 02:08:12

SPOJ Problem 1436:Is it a tree的相关文章

SPOJ QTREE 375. Query on a tree

SPOJ Problem Set (classical) 375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHA

素数筛法--SPOJ Problem 2 Prime Generator

质数(prime number)又称素数,除了1和它本身外,不能整除以其他自然数,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数.最小的质数是2. 要判断一个整数N是不是质数很简单,看它是否能被2到sqrt(N)之间的整数整除即可. def isPrime(n): if n%2==0: return False for i in xrange(3,int(math.sqrt(n)+1),2): if n%i==0: return False return True 不过要找出1

Problem - D - Codeforces Fix a Tree

Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作数. 如果有环和线,环被打开的同时,接入到线上.那就是线和环的总数-1. 如果只有环的话,把所有的环打开,互相接入,共需n次操作. #include <cstdio> #include <algorithm> using namespace std; const int maxn =

SPOJ Problem 7974:What&#39;s Next

求数列下一项,啥都不说了,贡献了N多WA... #include<cstdio> int main(){ int a,b,c; while(scanf("%d%d%d",&a,&b,&c)&&(a||b||c)){ if (b*2==c+a&&b!=a) printf("AP %d\n",2*c-b); else printf("GP %d\n",c*c/b); } return

SPOJ - QTREE 375 Query on a tree 树链剖分+线段树

操作1:修改第k条边权. 操作2:询问两点间最大边权. 树链剖分,然后线段树维护最大值 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map> #include<queue> #include<vector> #inclu

SPOJ Problem 1437:Longest path in a tree

求树的最长链,BFS和DFS都可以,时间复杂度O(n) #include<cstdio> #include<cstring> int tot,vt[20005],nxt[20005],a[20005]; bool vis[10005]; int n,i,j,xx,yy,s,ma,r; void search(int x,int dep){ int p; vis[x]=1; if (dep>ma){ma=dep;r=x;} p=a[x]; while(p){ if (!vis[

【树链剖分模板】【SPOJ 375】 Query on a tree

375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of

SPOJ Problem 2: Prime Generator

嗯..在SPOJ上刷的第二题. 一开始不知道哪错了,后来发现i出现了两遍.. 因为m<10^9,所以用素数筛筛32000以内的数,开一个4000的数组存储就行.然后再从n开始用素数筛,总之效率还行. 代码如下: //0.01s 3.2M #include<cstdio> #include<cstring> #include<cmath> int n,i,j,t,m,k,tot; int a[100005],b[4000]; int prime[40000]; in

SPOJ Problem 9948:Will it ever stop

如题..http://www.spoj.com/problems/WILLITST/ #include<cstdio> long long n; int main(){ scanf("%lld",&n); while(n>1){ if (n==3||n==6){printf("NIE\n");return 0;} if (n&1)n=(n+1)/2*3;else n>>=1; } printf("TAK\n&q