#67. New Year's cancer
Time Limit: 20 sec memory limit: 256 MB
Question connection
Http://uoj.ac/problem/67
Description when he quit the old and welcomed the new year, pleasant goat was taking care of the Green Belt in Yangcun, and then he found a tree with a cancer.
The tree with a tumor can be expressed in an undirected graph with n knots and M knots without an undirected edge. In this figure, some nodes are called cancer nodes, that is, after deleting the node and the adjacent edge, the figure will become a tree. Tree is a undirected connected graph without a simple ring.
Now I will give you this undirected graph. Pleasant goat asks you to help him find all the cancer nodes. Input
The first line has two positive integers, N and M, indicating that there are n vertices and m edges. Ensure n ≥ 2.
In the next m row, each line has two integers V and U, indicating that there is a undirected edge between V and U. 1 ≤ v, u ≤ n. Ensure that there is no duplicate edge or self-ring.
Output
The first line is a positive integer NS, indicating that the NS nodes in this graph are tumors.
The next line contains NS integers, each representing the number of a cancer node. Output Data in ascending order.
Data ensures that at least one cancer node exists in the graph.
Sample Input
6 6
1 2
1 3
2 4
2 5
4 6
5 6
Sample output
3
4 5 6
Hint
Question
Question:
You need to understand what a tree is. If you only understand the tree as a "guy who looks like a tree", you will be finished.
We need to define what is called a tree. We can understand that an undirected connected graph with n-1 edge. "With n-1 edge" indicates that we have n-2 edges in the end, so you need to delete a node with a degree of M-(N-2.
Consider the second condition, that is, the remaining graph is still connected after this vertex is deleted, so this vertex is not a cut vertex.
So we can use the Tarjan algorithm to find the cut point, and then output all the nodes that are not cut points and the degree meets the conditions. You can get 100 points. (It seems like this can magically pass through M = n−2 ...... 555 ......)
Code:
//qscqesze#include <cstdio>#include <cmath>#include <cstring>#include <ctime>#include <iostream>#include <algorithm>#include <set>#include <vector>#include <sstream>#include <queue>#include <typeinfo>#include <fstream>#include <map>#include <stack>typedef long long ll;using namespace std;//freopen("D.in","r",stdin);//freopen("D.out","w",stdout);#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)#define maxn 200001#define mod 10007#define eps 1e-9int Num;char CH[20];//const int inf=0x7fffffff; //нчоч╢Сconst int inf=0x3f3f3f3f;/*inline void P(int x){ Num=0;if(!x){putchar(‘0‘);puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts("");}*/inline ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f;}inline void P(int x){ Num=0;if(!x){putchar(‘0‘);puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts("");}//**************************************************************************************struct edge{ int v,next;};edge e[maxn];int cnt,head[maxn];void insert(int x,int y){ e[cnt].v=y; e[cnt].next=head[x]; head[x]=cnt; cnt++;}int n,m,times,rootCnt;int deg[maxn],dfn[maxn],low[maxn];bool state[maxn],cut[maxn];void tarjan(int u,int pre){ dfn[u]=low[u]=++times; state[u]=1; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].v; if(v==pre) continue; if(!state[v]) { tarjan(v,u); low[u]=min(low[u],low[v]); } else if(state[v]==1) low[u]=min(low[u],dfn[v]); } if(dfn[pre]<=low[u]) { if(pre==1) { rootCnt++; } else cut[pre]=1; }}int main(){ //freopen("test.txt","r",stdin); n=read(),m=read(); int a,b; for(int i=1;i<=n;i++) head[i]=-1; for(int i=0;i<m;i++) { a=read(),b=read(); insert(a,b); insert(b,a); deg[a]++; deg[b]++; } tarjan(1,0); if(rootCnt>1) cut[1]=1; int ans=0; for(int i=1;i<=n;i++) { if(!cut[i]&&(m-deg[i])==n-2) ans++; } printf("%d\n",ans); for(int i=1;i<=n;i++) if(!cut[i]&&(m-deg[i])==n-2) printf("%d ",i); printf("\n");}
Uoj 67 new year's cancer Tarjan cut point