Codeforces Round #407 (Div. 1) B. Weird journey
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.
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.
Print out the only integer — the number of good paths in Uzhlyandia.
5 4 1 2 1 3 1 4 1 5
6
5 3 1 2 2 3 4 5
0
2 2 1 1 1 2
1
分析:從一個點出發如果經過的所有邊都走了兩邊,那麼相當於最後回到了起點,簡單畫一下圖就會發現符合條件的邊對要麼共頂點,要麼有一條邊是自環.
#include <bits/stdc++.h>
#define N 1000005
#define INF 2147483647
using namespace std;
int n,m,cnt,u,v,root,vis[N],f[N],edg[N];
long long ans;
vector<int> G[N];
void dfs(int u)
{
vis[u] = true;
for(int i = 0;i < G[u].size();i++)
{
int v = G[u][i];
if(!vis[v]) dfs(v);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = 1;i <= m;i++)
{
scanf("%d%d",&u,&v);
edg[u] = edg[v] = true;
if(u == v)
{
cnt++;
continue;
}
G[u].push_back(v);
G[v].push_back(u);
f[u]++,f[v]++;
root = u;
}
dfs(u);
bool flag = true;
for(int i = 1;i <= n;i++)
if(edg[i]) flag &= vis[i];
for(int i = 1;i <= n;i++) ans += 1ll*f[i]*(f[i]-1)/2;
cout<<flag*(ans+1ll*cnt*(m-cnt)+1ll*cnt*(cnt-1)/2)<<endl;
}
相關文章
- Codeforces Round #407 (Div. 1) C. The Great Mixing(bfs)
- Codeforces Round #491 (Div. 2) B. Getting an A
- Codeforces Round #242 (Div. 2) B. Megacity
- Codeforces Round #249 (Div. 2) B. Pasha Maximizes
- Codeforces Round #252 (Div. 2) B. Valera and FruitsUI
- Codeforces Round #246 (Div. 2) B. Football Kit
- Codeforces Round #244 (Div. 2) B. Prison Transfer
- Codeforces Round #228 (Div. 2) B. Fox and CrossROS
- Codeforces Round #215 (Div. 2) B. Sereja and Suffixes
- Codeforces Round #243 (Div. 2) B. Sereja and Mirroring
- Codeforces Round #196 (Div. 2) B. Routine Problem
- Codeforces Round #251 (Div. 2) B. Devu, the Dumb Guydev
- Codeforces Round #209 (Div. 2) B. Permutation
- Codeforces Round 934 (Div. 1)
- Codeforces Round #676 (Div. 2) B. Putting Bricks in the Wall
- Codeforces Round #213 (Div. 2) B. The Fibonacci Segment
- Codeforces Round #248 (Div. 2) B. Kuriyama Mirai's StonesAI
- Codeforces Round #216 (Div. 2) B. Valera and Contest
- Codeforces Round 928 (Div. 4) B. Vlad and Shapes
- Codeforces Round 947 (Div. 1 + Div. 2)
- Codeforces Round #406 (Div. 1) B. Legacy(線段樹優化建圖)優化
- Codeforces Round #235 (Div. 2) B. Sereja and Contests
- Codeforces Round #233 (Div. 2) B. Red and Blue Balls
- Codeforces Round #192 (Div. 2) B. Road ConstructionStruct
- Codeforces Round 709 (Div. 1)
- Codeforces Round 969 (Div. 1) 記錄
- Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2)
- 【Codeforces Round #499 (Div. 1) B】Rocket
- Codeforces Round #537 (Div. 2)B. Average Superhero Gang Power (貪心+暴力)
- Codeforces Beta Round #76 (Div. 1 Only) A. Frames
- Codeforces Round 943 (Div. 3)(C-G1)
- Codeforces Testing Round #10 B. Balancer
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #690 (Div. 3)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)