Codeforces Round #600 (Div. 2) D. Harmonious Graph

Topic Address: http://codeforces.com/contest/1253/problem/D

Meaning of the questions: give you two numbers, n and m. Represent n points, m edges. If l and n r represents the two points a, and the two communication points, at (l, r) in any number to communicate with the l, then it does not satisfy the plus side, the number of edges added to satisfy the minimum Q condition.

Ideas: dfs already interlinked points, plus a mark, find the maximum point of the current collection of x, and x is less than the set not connected to this point is to add an edge, if the set itself has also be dfs incorporated directly after edging.

AC Code:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 using namespace std;
 7 typedef long long ll;
 8 const int N=200005;
 9 int mk[N],n,m,cnt=1;
10 int num=1;
11 vector<int>g[N];
12 void dfs(int tm){
13     MK [(TM)] = . 1 ;
 14      CNT = max (CNT, (TM));
 15      // DFS all points connected thereto, the total number of edges is not more than 200,000, there is no fear timeout 
16      for (Auto I: G [(TM)] ) IF (MK [I]) DFS (I);!   // Auto may instead int 
. 17  }
 18 is  void Sol () {
 . 19      int ANS = 0 ;
 20 is      the while (NUM <= n-) {
 21 is          the while (MK [NUM ]) NUM ++;    // iterate to find the non-communicating 
22 is          IF (NUM <CNT) ANS ++ ;
 23 is          DFS (NUM);
 24      }
 25     cout<<ans<<endl;
26 }
27 int main(){
28     cin>>n>>m;
29     int u,v;
30     for(int i=0;i<m;i++){
31         cin>>u>>v;
32         g[u].push_back(v);
33         g[v].push_back(u);
34     }
35     sol();
36     return 0;
37 }

 

auto: This thing is really easy to use, the code is not long, but a large role.

Auto know the type defined after the element can be used instead of the keywords, where the auto also changed int row. But so what harm and benefits we do not know, still learning, understanding.

Guess you like

Origin www.cnblogs.com/xunzf0402/p/11892272.html