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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

NYOJ 920 Trees

發布時間:2025/3/16 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NYOJ 920 Trees 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Trees

時間限制:1000?ms ?|? 內存限制:65535?KB 難度:2 描述
A graph consists of a set vertices and edges between pairs of vertices. Two vertices are connected if there is a path(subset of edges)leading from one vertex to another, and a connected component is a maximal subset of vertices that are all connected to each other. A graph consists one or more connected components. A tree is a connected component without cycles, but it can also be characterized in other ways. For example, a tree consisting of n vertices has exactly n-1 edges.Also, there is a unique path connecting any pair of vertices in a tree. Give a graph, report the number of connected components that are also trees.? 輸入
The input consist of a number of cases. Each case starts with two non-negative integer n and m, satisfying n <= 500 and m <= n(n-1)/2. This is followed by m lines,each containing two integers specifying the two vertices connected by an edge. The vertices are labeled from 1 to n. The end of input is indicated by a line containing n = m = 0.
輸出
For each case,print one of the following lines depending on how?
  many different connected components are trees.(T > 1 below):
   Case x: A forest of T trees.
   Case x: There is one tree.
   Case x: No Trees.
  x is the case number (starting from 1).
  
樣例輸入
6 3 1 2 2 3 3 4 6 5 1 2 2 3 3 4 4 5 5 6 6 6 1 2 2 3 1 3 4 5 5 6 6 4 0 0
樣例輸出
Case 1: A forest of 3 trees. Case 2: There is one tree. Case 3: No Trees.

題意:給出一張由n個點和m條邊構成的無向圖,不是連通的,判斷圖的每一部分是否是一個樹,即圖中有幾棵樹。

滿足下列條件可以的點和邊可以構成一棵樹:1.n個點由n-1條邊相連,任意兩個點之間只有一條邊相連。

解題思路:用并查集把各個部分找出來,對于每一部分判斷所有點的度之和與點個數的關系,

如果度之和等于點數*2-2,則可以構成一棵樹。

#include<cstdio> #include<algorithm> #include<vector> using namespace std; const int N = 5e2 + 10; int father[N], deg[N]; vector<int> vec[N];void Initial(int n) //初始化 {for(int i = 1; i <= n; i++)father[i] = i, deg[i] = 0; }int Find(int x) //尋找父節點 {if(father[x] != x)father[x] = Find(father[x]);return father[x]; }void Union(int a, int b) //合并兩個集合 {int p = Find(a);int q = Find(b);if(p != q)father[p] = q; }int main() {int n, m, i, j, cas = 1;while(~scanf("%d%d",&n,&m) && (n+m)){Initial(n); //并查集初始化int u, v;for(i = 0; i < m; i++){scanf("%d%d",&u,&v);deg[u]++;deg[v]++;Union(u,v);} //求每個點的度數for(i = 1; i <= n; i++){vec[i].clear();//刪除容器中保存的所有元素if(Find(i) == i){for(j = 1; j <= n; j++){if(Find(j) == Find(i))vec[i].push_back(j);}}}//找出哪些點屬于同一個集合int ans = 0;for(i = 1; i <= n; i++){int cnt = vec[i].size();if(cnt == 0)continue;int sum = 0;for(j = 0; j < cnt; j++){sum += deg[vec[i][j]];}//求集合中點的度數之和if(sum == cnt * 2 - 2)ans++; //圖中無環}printf("Case %d: ",cas++);if(ans == 0)printf("No Trees.\n");else if(ans == 1)printf("There is one tree.\n");elseprintf("A forest of %d trees.\n",ans);}return 0; }

總結

以上是生活随笔為你收集整理的NYOJ 920 Trees的全部內容,希望文章能夠幫你解決所遇到的問題。

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