【CodeForces - 789D】Weird journey(思维,图的性质,tricks,有坑)
題干:
Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherland?— Uzhlyandia.
It is widely known that Uzhlyandia has?n?cities connected with?m?bidirectional roads. Also, there are no two roads in the country that connect the same pair of cities, but roads starting and ending in the same city can exist. Igor wants to plan his journey beforehand. Boy thinks a path is?good?if the path goes over?m?-?2?roads twice, and over the other?2?exactly once. The good path can start and finish in any city of Uzhlyandia.
Now he wants to know how many different good paths are in Uzhlyandia. Two paths are considered different if the sets of roads the paths goes over exactly once differ. Help Igor?— calculate the number of good paths.
Input
The first line contains two integers?n,?m?(1?≤?n,?m?≤?106)?— the number of cities and roads in Uzhlyandia, respectively.
Each of the next?m?lines contains two integers?u?and?v?(1?≤?u,?v?≤?n) that mean that there is road between cities?u?and?v.
It is guaranteed that no road will be given in the input twice. That also means that for every city there is no more than one road that connects the city to itself.
Output
Print out the only integer?— the number of good paths in Uzhlyandia.
Examples
Input
5 4 1 2 1 3 1 4 1 5Output
6Input
5 3 1 2 2 3 4 5Output
0Input
2 2 1 1 1 2Output
1Note
In first sample test case the good paths are:
- 2?→?1?→?3?→?1?→?4?→?1?→?5,
- 2?→?1?→?3?→?1?→?5?→?1?→?4,
- 2?→?1?→?4?→?1?→?5?→?1?→?3,
- 3?→?1?→?2?→?1?→?4?→?1?→?5,
- 3?→?1?→?2?→?1?→?5?→?1?→?4,
- 4?→?1?→?2?→?1?→?3?→?1?→?5.
There are good paths that are same with displayed above, because the sets of roads they pass over once are same:
- 2?→?1?→?4?→?1?→?3?→?1?→?5,
- 2?→?1?→?5?→?1?→?3?→?1?→?4,
- 2?→?1?→?5?→?1?→?4?→?1?→?3,
- 3?→?1?→?4?→?1?→?2?→?1?→?5,
- 3?→?1?→?5?→?1?→?2?→?1?→?4,
- 4?→?1?→?3?→?1?→?2?→?1?→?5,
- and all the paths in the other direction.
Thus, the answer is?6.
In the second test case, Igor simply can not walk by all the roads.
In the third case, Igor walks once over every road.
題目大意:
小男孩Igor想成為一名旅行者。起初,他決定訪問他祖國?— Uzhlyandia的所有城市。
眾所周知,Uzhlyandia有?n?座城市,用?m?條無向邊連接起來。 此外,沒有兩條道路連接同一對城市,但道路開始和結束在同一個城市可以存在。Igor想事先計劃好他的旅行。男孩認為好的路徑經過?m?-?2?條邊恰好兩次,經過?2?條邊恰好一次, 這條路徑的起點和終點可以在?n?個城市中任選。
現在他想知道有多少條不同的路徑滿足要求。注意,經過點順序不同,每條邊經過次數相同的路徑為同一條路徑。
Input
第一行包含兩個整數?n,?m?(1?≤?n,?m?≤?106)?— Uzhlyandia的城市和道路的數量。
之后?m?行包含兩個整數?u?和?v?(1?≤?u,?v?≤?n) 表示城市?u?和?v之間有一條路徑。
保證沒有重邊,但可能有自環。
解題報告:
貼一個題解:m-2條邊走兩次,2條邊走一次,那么我們可以把每一條邊都復制一遍,然后再刪掉兩條邊,求歐拉通路的方案數。
另一種思考方式:假設走一次的這兩條邊稱為關建邊,那么需要發現你選的這兩條關建邊,一定連在一個頂點上。所以枚舉每個點C(n,2)統計一下,然后對于自環單獨容斥處理一下即可。
當然,對于不連通的圖,直接輸出0就可以了。但是這里有坑,就是他雖然不連通,但是如果獨立出去的分量都是獨立的點的話,是沒有關系的,因為他們并沒有邊相連。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e6 + 5; vector<int> vv[MAX]; int n,m,f[MAX],vis[MAX],cnt; void dfs(int x,int fa) {vis[x] = 1;for(auto v : vv[x]) {if(vis[v] || v == fa) continue;dfs(v,x);} } int main() {cin>>n>>m;for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v);if(u == v) vv[u].pb(v);else vv[u].pb(v),vv[v].pb(u);}for(int i = 1; i<=n; i++) sort(vv[i].begin(),vv[i].end()),f[i]=i;ll ans = 0; for(int i = 1; i<=n; i++) {if(vv[i].size() > 0) {dfs(i,0);break;}}for(int i = 1; i<=n; i++) {if(vis[i] == 0 && vv[i].size() > 0) return 0,puts("0");}for(int i = 1; i<=n; i++) {int ok = binary_search(vv[i].begin(),vv[i].end(),i);cnt += ok;ll tmp = vv[i].size() - ok;ans += tmp*(tmp-1)/2;}printf("%lld\n",ans +1LL*cnt*(m-1) - 1LL*cnt*(cnt-1)/2);return 0 ; }?
總結
以上是生活随笔為你收集整理的【CodeForces - 789D】Weird journey(思维,图的性质,tricks,有坑)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 兴业信用卡费率优惠活动 兴业信用卡费率最
- 下一篇: 拜登当选对美元有什么影响?美元会大幅贬值