Codeforces 479【E】div3

Topic link: http://codeforces.com/problemset/problem/977/E

 

The meaning of the question: It is to give you connected edges, and let you ask for how many cycles there are in the graph.

 

Solution: I am very poor in graph theory, and generally do not know how to do graph theory problems. QAQ looks at the official questions that have been solved. Probably if this is a ring, the degree of each point should be 2, and dfs is marked according to this.

 

Even if it is a simple graph theory, you will still be confused when you see it. QWQ. In the future, I will write more dfs and graph theory. Personally, though, I prefer number theory.

 

 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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325850501&siteId=291194637