1. 程式人生 > >Codeforces 479【E】div3

Codeforces 479【E】div3

codeforce 應該 isp 個人 標記 () def 數論 ==

題目鏈接:http://codeforces.com/problemset/problem/977/E

題意:就是給你相連邊,讓你求圖內有幾個環。

題解:我圖論很差,一般都不太會做圖論的題。QAQ看官方題解過的。大概就是如果這是一個環的話,每一個點的度數都應該是2才對,根據這個進行dfs做標記。

就算是個簡單圖論,看到還是會一臉懵逼。QWQ。以後還是會多多寫dfs和圖論啦。不過個人還是更喜歡數論什麽的。

技術分享圖片
 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4
const int maxn = 200005; 5 6 vector<int> g[maxn]; 7 bool vis[maxn]; 8 int flag; 9 10 void dfs(int x){ 11 if(vis[x]) 12 return ; 13 if(g[x].size() != 2){ 14 flag = 1; 15 } 16 vis[x] = true; 17 for(int i = 0; i < g[x].size() ;i++){ 18 dfs(g[x][i]);
19 } 20 21 } 22 23 int main(){ 24 int n,m; 25 cin>>n>>m; 26 while(m--){ 27 int x,y; 28 cin>>x>>y; 29 g[x].push_back(y); 30 g[y].push_back(x); 31 } 32 int ans = 0; 33 for(int i = 1; i <= n ;i++){
34 if(!vis[i]){ 35 flag = 0; 36 dfs(i); 37 if(flag == 0){ 38 ans++; 39 } 40 } 41 } 42 cout<<ans<<endl; 43 return 0; 44 }
View Code

Codeforces 479【E】div3