【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?條無向邊連接起來。 此外,沒有兩條道路連接同一對城市,但道路開始和結(jié)束在同一個(gè)城市可以存在。Igor想事先計(jì)劃好他的旅行。男孩認(rèn)為好的路徑經(jīng)過?m?-?2?條邊恰好兩次,經(jīng)過?2?條邊恰好一次, 這條路徑的起點(diǎn)和終點(diǎn)可以在?n?個(gè)城市中任選。
現(xiàn)在他想知道有多少條不同的路徑滿足要求。注意,經(jīng)過點(diǎn)順序不同,每條邊經(jīng)過次數(shù)相同的路徑為同一條路徑。
Input
第一行包含兩個(gè)整數(shù)?n,?m?(1?≤?n,?m?≤?106)?— Uzhlyandia的城市和道路的數(shù)量。
之后?m?行包含兩個(gè)整數(shù)?u?和?v?(1?≤?u,?v?≤?n) 表示城市?u?和?v之間有一條路徑。
保證沒有重邊,但可能有自環(huán)。
解題報(bào)告:
貼一個(gè)題解:m-2條邊走兩次,2條邊走一次,那么我們可以把每一條邊都復(fù)制一遍,然后再刪掉兩條邊,求歐拉通路的方案數(shù)。
另一種思考方式:假設(shè)走一次的這兩條邊稱為關(guān)建邊,那么需要發(fā)現(xiàn)你選的這兩條關(guān)建邊,一定連在一個(gè)頂點(diǎn)上。所以枚舉每個(gè)點(diǎn)C(n,2)統(tǒng)計(jì)一下,然后對于自環(huán)單獨(dú)容斥處理一下即可。
當(dāng)然,對于不連通的圖,直接輸出0就可以了。但是這里有坑,就是他雖然不連通,但是如果獨(dú)立出去的分量都是獨(dú)立的點(diǎn)的話,是沒有關(guān)系的,因?yàn)樗麄儾]有邊相連。
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 ; }?
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 789D】Weird journey(思维,图的性质,tricks,有坑)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 兴业信用卡费率优惠活动 兴业信用卡费率最
- 下一篇: 拜登当选对美元有什么影响?美元会大幅贬值