☆【CodeForces - 764C】Timofey and a tree (思维题,树的性质)
題干:
Each New Year Timofey and his friends cut down a tree of?n?vertices and bring it home. After that they paint all the?n?its vertices, so that the?i-th vertex gets color?ci.
Now it's time for Timofey birthday, and his mother asked him to remove the tree. Timofey removes the tree in the following way: he takes some vertex in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex. After that Timofey brings the tree to a trash can.
Timofey doesn't like it when many colors are mixing together. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him. He doesn't consider the whole tree as a subtree since he can't see the color of the root vertex.
A subtree of some vertex is a subgraph containing that vertex and all its descendants.
Your task is to determine if there is a vertex, taking which in hands Timofey wouldn't be annoyed.
Input
The first line contains single integer?n?(2?≤?n?≤?105)?— the number of vertices in the tree.
Each of the next?n?-?1?lines contains two integers?u?and?v?(1?≤?u,?v?≤?n,?u?≠?v), denoting there is an edge between vertices?u?and?v. It is guaranteed that the given graph is a tree.
The next line contains?n?integers?c1,?c2,?...,?cn?(1?≤?ci?≤?105), denoting the colors of the vertices.
Output
Print "NO" in a single line, if Timofey can't take the tree in such a way that it doesn't annoy him.
Otherwise print "YES" in the first line. In the second line print the index of the vertex which Timofey should take in hands. If there are multiple answers, print any of them.
Examples
Input
4 1 2 2 3 3 4 1 2 1 1Output
YES 2Input
3 1 2 2 3 1 2 3Output
YES 2Input
4 1 2 2 3 3 4 1 2 1 2Output
NO題目大意:
? ? 給定一棵每個頂點都標記顏色的樹(無根樹),問能否從一個頂點出發(以他為根節點),使得它的子樹都是一樣顏色(不包括這個頂點本身的顏色)詳見第二個樣例。
題解:因條件要求,我們可以知道不同的顏色的頂點中其中一個必須是答案頂點,我們統計一下不同顏色頂點個數(設為m),和不同顏色頂點中頂點出現次數。最后如果有一個頂點出現次數等于m,那么這個頂點就是答案頂點。
解題報告:
? ? 思維題啊害死我、、、
? ? 第一反應子樹,我擦dfs序吧?
? ? 第二反應不行啊總不能對每一個頂點建樹吧?要不試試、、
? ? 第三反應woc超時了,,,? ??
? ? 關于圖論的問題,可以考慮cnt計數、、、這題sum表示每一條邊中有多少條邊是兩節點顏色不一樣的。如果正好和節點 i 的邊數相同,那 i 就是答案。
AC代碼:
#include<bits/stdc++.h>using namespace std; const int MAX = 1e5 + 5 ; int u[MAX],v[MAX],cnt[MAX],col[MAX]; int main() {int n,sum=0;cin>>n;for(int i = 1; i<n; i++) {scanf("%d%d",&u[i],&v[i]);}for(int i = 1; i<=n; i++) {scanf("%d",&col[i]);}for(int i = 1; i<n; i++) {if(col[u[i]]!=col[v[i]]) {sum++;cnt[u[i]]++;cnt[v[i]]++;}}for(int i = 1; i<=n; i++) {if(cnt[i] == sum) {printf("YES\n%d\n",i);return 0 ;}}printf("NO\n");return 0 ; }dfs序超時代碼:(剛開始是o(n)的注釋掉那一部分,后來改成logn的線段樹,但是還是超時,看來時間都花在dfs序上了)
#include<bits/stdc++.h>using namespace std; const int MAX = 100000 +5; const int INF = 0x3f3f3f3f; int cnt,id; int st[MAX],ed[MAX],col[MAX],q[MAX]; int head[MAX]; struct Edge{int to,ne; }e[1000000 + 5]; struct TREE {int l,r;int maxx,minn; } tree[MAX * 4]; void dfs(int x,int fa) {q[++id]=x;st[x]=id;for(int i=head[x];i!=-1;i=e[i].ne)if(e[i].to!=fa)dfs(e[i].to,x);ed[x]=id; } void pushup(int cur) {tree[cur].maxx = max(tree[cur*2].maxx,tree[cur*2+1].maxx);tree[cur].minn = min(tree[cur*2].minn,tree[cur*2+1].minn); } void build(int l,int r,int cur) {tree[cur].l=l;tree[cur].r=r;if(l == r) {tree[cur].maxx = col[q[r]];tree[cur].minn = col[q[r]];return ;}int m = (l + r)/2;build(l,m,cur*2);build(m+1,r,cur*2+1);pushup(cur); } int qmax(int pl,int pr,int cur) {if(pl <= tree[cur].l && pr >= tree[cur].r) return tree[cur].maxx;int ll=0,rr=0;if(pl <= tree[cur*2].r) ll = qmax(pl,pr,cur*2);if(pr >= tree[cur*2+1].l) rr = qmax(pl,pr,cur*2+1);return max(rr,ll); } int qmin(int pl,int pr,int cur) {if(pl <= tree[cur].l && pr >= tree[cur].r) return tree[cur].minn;int ll=INF,rr=INF;if(pl <= tree[cur*2].r) ll = qmin(pl,pr,cur*2);if(pr >= tree[cur*2+1].l) rr = qmin(pl,pr,cur*2+1);return min(rr,ll); } void add(int u,int v) {e[++cnt].to = v;e[cnt].ne = head[u];head[u] = cnt; } int main() {int n,u,v;scanf("%d",&n);memset(head,-1,sizeof head);for(int i = 1; i<n; i++) {scanf("%d%d",&u,&v);add(u,v);add(v,u);}for(int i = 1; i<=n; i++) {scanf("%d",&col[i]);}//枚舉每一個頂點int flag = 1; for(int i = 1; i<=n; i++) {id=0;dfs(i,i);flag = 1;build(1,n,1);for(int j = head[i]; j!=-1; j=e[j].ne) {int tmp = col[e[j].to];int maxx = qmax(st[e[j].to],ed[e[j].to],1);int minn = qmin(st[e[j].to],ed[e[j].to],1);if(maxx != tmp || minn != tmp) flag=0; // for(int k = st[e[j].to]; k<=ed[e[j].to]; k++) { // if(col[q[k]] != tmp) { // flag=0;break; // } // }if(flag == 0) break;}if(flag == 1) {printf("YES\n");printf("%d\n",i);return 0;}}printf("NO\n");return 0 ; }或者用并查集縮圖https://www.cnblogs.com/jasonlixuetao/p/6401831.html
并查集思路: 先建圖,遍歷每個結點,統計每個結點的度,對結點i,若其相鄰的結點j與其顏色相同,那么將其合并,并將結點i和結點j的度都減1 ,合并完后,所有顏色相同且相互鄰接的點合并為一個集合,統計集合個數cnt,將一個集合看作一個點,在這個虛圖中滿足條件的點i 一定滿足cnt-1==deg[i],在合并后的原圖中所有點中尋找滿足cnt-1==deg[i]的點。 #include<iostream> #include<cstdio> #include<algorithm> #include<vector> using namespace std; #define N 100005 struct Node {int p,d; } deg[N]; vector<int> gra[N]; int col[N]; bool cmp(Node a,Node b) {return a.d<b.d; } int father[N]; int wei[N]; int Find(int a) {if(father[a]!=a)father[a]=Find(father[a]);return father[a]; } void Merge(int a,int b) {int fa=Find(a);int fb=Find(b);if(fa!=fb) {father[fa]=fb;wei[fb]+=wei[fa];} } int main() {int n;scanf("%d",&n);int a,b;for(int i=1; i<=n; i++) {deg[i].p=i;father[i]=i;wei[i]=1;}for(int i=0; i<n-1; i++) {scanf("%d%d",&a,&b);deg[a].d++;deg[b].d++;gra[a].push_back(b);gra[b].push_back(a);}for(int i=1; i<=n; i++)scanf("%d",&col[i]);for(int i=1; i<=n; i++)for(int j=0; j<gra[i].size(); j++) {if(col[i]==col[gra[i][j]]) {if(Find(i)!=Find(gra[i][j])) {Merge(i,gra[i][j]);deg[i].d--;deg[gra[i][j]].d--;}}}int cnt=0;for(int i=1; i<=n; i++)if(father[i]==i)cnt++;for(int i=1; i<=n; i++) {if(cnt==deg[i].d+1) {printf("YES\n%d\n",i);return 0;}}printf("NO\n");return 0; }?
總結
以上是生活随笔為你收集整理的☆【CodeForces - 764C】Timofey and a tree (思维题,树的性质)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: qtaet2s.exe - qtaet2
- 下一篇: 油价暴涨 希腊电视台教观众使用软管偷汽油