日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces 982 C. Cut 'em all! 图的遍历

發布時間:2024/10/6 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces 982 C. Cut 'em all! 图的遍历 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
點擊打開鏈接
C. Cut 'em all!time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

You're given a tree with?n vertices.

Your task is to determine the maximum possible number of edges that can be removed?

in such a way that all the remaining connected components will have even size.

Input

The first line contains an integer?n?(1n105) denoting the size of the tree.

The next?n?1?lines contain two integers?u,?v?(1u,vn) each, describing the vertices connected by the?i-th edge.

It's guaranteed that the given edges form a tree.

Output

Output a single integer?k?— the maximum number of edges that can be removed to leave all connected components with even size, or??1

if it is impossible to remove edges in order to satisfy this property.

ExamplesinputCopy4 2 4 4 1 3 1 outputCopy1inputCopy3 1 2 1 3 outputCopy-1inputCopy10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 outputCopy4inputCopy2 1 2 outputCopy0 Note

In the first example you can remove the edge between vertices?1 and?4.?

The graph after that will have two connected components with two vertices in each.

In the second example you can't remove edges in such a way that all components have even number of vertices,?

so the answer is??1

.

題目描述:

? ? 給你一個有n個結點的樹,其中有n-1條邊,詢問你最多你可以移動多少條邊,使得每個強連通分量的個數均為偶數。

題目分析:

? ? 因為我們知道,如果所給的結點的個數是奇數,那么,因為奇數必定是由一個偶數和一個奇數組成,故總會有一個強連通分量的個數為奇數,因此當n為奇數使,答案為-1。

? ? 那么當n等于偶數的時,我們只需要進行dfs遍歷整張圖,統計一下某個結點以及其兒子結點的數量,如果遞歸到最底時統計出的數量為偶數,則答案+1即可。

#include<bits/stdc++.h> using namespace std; const int maxn=2e5+5; struct edge {int to,next; }q[maxn]; int head[maxn]; int size[maxn]; int cnt=0; int ans=0; void init() {memset(head,-1,sizeof(head));cnt=0; } void add_edge(int from,int to) {q[cnt].next=head[from];q[cnt].to=to;head[from]=cnt++; } void dfs(int x,int fa) {size[x]=1;for(int i=head[x];i!=-1;i=q[i].next){if(q[i].to==fa) continue;dfs(q[i].to,x);size[x]+=size[q[i].to];}if(size[x]%2==0){ans++;size[x]=0;} } int main() {int n,u,v;scanf("%d",&n);init();for(int i=0;i<n-1;i++){scanf("%d%d",&u,&v);add_edge(u,v);add_edge(v,u);}if(n&1){printf("-1\n");return 0;}dfs(1,-1);printf("%d\n",ans-1);return 0; }


#include<bits/stdc++.h> using namespace std; const int maxn=1e5+5; vector<int>G[maxn]; int son[maxn]; int n,ans; void dfs(int x,int fa) {son[x]=1;for(int i=0;i<G[x].size();i++){int k=G[x][i];if(k!=fa){dfs(k,x);son[x]+=son[k];}}if(son[x]%2==0){ans++;son[x]=0;} } int main() {ans=0;memset(son,0,sizeof(son));scanf("%d",&n);for(int i=0;i<n-1;i++){int a,b;scanf("%d%d",&a,&b);G[a].push_back(b);G[b].push_back(a);}if(n%2==1){printf("-1\n");return 0;}dfs(1,-1);printf("%d\n",ans-1);return 0; }


總結

以上是生活随笔為你收集整理的Codeforces 982 C. Cut 'em all! 图的遍历的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。