Submission #140115

# Submission time Handle Problem Language Result Execution time Memory
140115 2019-08-02T05:59:51 Z KLPP Mergers (JOI19_mergers) C++14
100 / 100
1219 ms 146124 KB
#include<bits/stdc++.h>

using namespace std;
typedef long long int lld;
typedef pair<lld,lld> pii;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define trav(a,v) for(auto a:v)
#define INF 1000000000000000000
int n,k;
vector<int> nei[1000000];
int state[1000000];
vector<int> states[1000000];
int dfs_num[1000000];
int dfs_low[1000000];
stack<int> s;
int parent[1000000];
int counter;
int SCC_count;
int SCC[1000000];
int deg[1000000];
bool visited[1000000];
int parent_tree[1000000];
void DFS(int node){
  dfs_num[node]=counter;
  dfs_low[node]=counter;
  counter++;
  s.push(node);
  visited[node]=true;
  trav(a,nei[node]){
    if(dfs_num[a]==-1){
      parent[a]=node;
      DFS(a);
      dfs_low[node]=min(dfs_low[node],dfs_low[a]);
    }else{
      if(parent[node]!=a && visited[a]){
	dfs_low[node]=min(dfs_low[node],dfs_num[a]);
      }
    }
  }
  if(dfs_low[node]==dfs_num[node]){
    while(true){
      int u=s.top();
      s.pop();
      visited[u]=false;
      SCC[u]=SCC_count;
      dfs_num[u]=-1;
      if(u==node){
	SCC_count++;
	return;
      }
    }
  }
}
void DFS2(int node){
  visited[node]=true;
  trav(a,nei[node]){
    if(!visited[a]){
      parent_tree[a]=node;
      DFS2(a);
    }
  }
}
class DSU{
  int parent[1000000];
  int size[1000000];
public:
  void init(int K){
    rep(i,0,K){
      parent[i]=i;
      size[i]=1;
    }
  }
  int root(int node){
    if(parent[node]==node)return node;
    parent[node]=root(parent[node]);
    return parent[node];
  }
  void merge(int a, int b){
    a=root(a);
    b=root(b);
    if(a==b)return;
    if(size[a]<size[b])swap(a,b);
    size[a]+=size[b];
    parent[b]=a;
  }
};
int main(){
  scanf("%d %d",&n,&k);
  rep(i,0,n)visited[i]=false;
  vector<pii> edges;
  rep(i,0,n-1){
    int x,y;
    scanf("%d %d",&x,&y);
    x--;y--;
    nei[x].push_back(y);
    nei[y].push_back(x);
    edges.push_back(pii(x,y));
    edges.push_back(pii(y,x));
  }
  DFS2(0);
  rep(i,0,n){
    scanf("%d",&state[i]);
    state[i]--;
    states[state[i]].push_back(i);
  }
  rep(i,0,k){
    rep(j,0,states[i].size()-1){
      pii new_edge=pii(states[i][j],states[i][j+1]);
      if(parent_tree[new_edge.first]!=new_edge.second && parent_tree[new_edge.second]!=new_edge.first){
	nei[states[i][j]].push_back(states[i][j+1]);
	nei[states[i][j+1]].push_back(states[i][j]);
      }
      //edges.push_back(pii(states[i][j],states[i][j+1]));
    }
  }
  /*bool important[edges.size()];
  rep(i,0,edges.size())important[i]=true;
  rep(i,0,edges.size()){
    rep(j,0,n){
      visited[j]=false;
      nei[j].clear();
    }
    rep(j,0,edges.size()){
      if(j!=i){
	nei[edges[j].first].push_back(edges[j].second);
	nei[edges[j].second].push_back(edges[j].first);
      }
    }
    DFS2(0);
    rep(j,0,n){
      
      important[i]=important[i]&visited[j];
    }
  }*/
  /*rep(i,0,n-1)cout<<important[i];
  cout<<endl;*/
  /*rep(i,0,edges.size()){
    if(important[i]){
      nei[edges[i].first].push_back(edges[i].second);
      nei[edges[i].second].push_back(edges[i].first);
    }
  }
  rep(i,0,n){
    trav(a,nei[i])cout<<a<<" ";
    cout<<endl;
  }cout<<endl;*/
  rep(i,0,n){
    dfs_low[i]=-1;
    dfs_num[i]=-1;
    visited[i]=false;
  }
  counter=0;
  SCC_count=0;
  DFS(0);
  //rep(i,0,n)cout<<SCC[i]<<endl;
  
  /*rep(i,0,n){
    visited[i]=false;
  }*/
  /*SCC_count=0;
  rep(i,0,n){
    if(!visited[i]){
      DFS2(i);
      while(!s.empty()){
	SCC[s.top()]=SCC_count;
	s.pop();
      }
      SCC_count++;
    }
    }*/
  //rep(i,0,n)cout<<SCC[i]<<endl;
  DSU *D=new DSU();
  D->init(SCC_count);
  rep(i,0,k){
    rep(j,0,states[i].size()-1){
      pii new_edge=pii(states[i][j],states[i][j+1]);
      if(!(parent_tree[new_edge.first]!=new_edge.second && parent_tree[new_edge.second]!=new_edge.first)){
	D->merge(SCC[new_edge.first],SCC[new_edge.second]);
      }
      //edges.push_back(pii(states[i][j],states[i][j+1]));
    }
  }
  rep(i,0,n)deg[i]=0;
  trav(a,edges){
    a.first=D->root(SCC[a.first]);
    a.second=D->root(SCC[a.second]);
    if(a.first!=a.second){
      //cout<<a.first<<" "<<a.second<<endl;
      deg[a.first]++;
      deg[a.second]++;
    }
  }
  int leaves=0;
  rep(i,0,SCC_count){
    if(deg[i]==2)leaves++;
  }
  printf("%d\n",(leaves+1)/2);
  return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:6:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,a,b) for(int i=a;i<b;i++)
mergers.cpp:107:9:
     rep(j,0,states[i].size()-1){
         ~~~~~~~~~~~~~~~~~~~~~~   
mergers.cpp:107:5: note: in expansion of macro 'rep'
     rep(j,0,states[i].size()-1){
     ^~~
mergers.cpp:6:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,a,b) for(int i=a;i<b;i++)
mergers.cpp:175:9:
     rep(j,0,states[i].size()-1){
         ~~~~~~~~~~~~~~~~~~~~~~   
mergers.cpp:175:5: note: in expansion of macro 'rep'
     rep(j,0,states[i].size()-1){
     ^~~
mergers.cpp:88:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&n,&k);
   ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:93:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&x,&y);
     ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:102:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&state[i]);
     ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 52 ms 55160 KB Output is correct
2 Correct 52 ms 55132 KB Output is correct
3 Correct 58 ms 55160 KB Output is correct
4 Correct 52 ms 55132 KB Output is correct
5 Correct 60 ms 55160 KB Output is correct
6 Correct 52 ms 55164 KB Output is correct
7 Correct 52 ms 55160 KB Output is correct
8 Correct 52 ms 55164 KB Output is correct
9 Correct 52 ms 55160 KB Output is correct
10 Correct 54 ms 55160 KB Output is correct
11 Correct 52 ms 55160 KB Output is correct
12 Correct 52 ms 55160 KB Output is correct
13 Correct 52 ms 55160 KB Output is correct
14 Correct 52 ms 55160 KB Output is correct
15 Correct 53 ms 55288 KB Output is correct
16 Correct 52 ms 55160 KB Output is correct
17 Correct 53 ms 55160 KB Output is correct
18 Correct 52 ms 55264 KB Output is correct
19 Correct 53 ms 55288 KB Output is correct
20 Correct 52 ms 55160 KB Output is correct
21 Correct 52 ms 55184 KB Output is correct
22 Correct 52 ms 55160 KB Output is correct
23 Correct 52 ms 55288 KB Output is correct
24 Correct 53 ms 55160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 55160 KB Output is correct
2 Correct 52 ms 55132 KB Output is correct
3 Correct 58 ms 55160 KB Output is correct
4 Correct 52 ms 55132 KB Output is correct
5 Correct 60 ms 55160 KB Output is correct
6 Correct 52 ms 55164 KB Output is correct
7 Correct 52 ms 55160 KB Output is correct
8 Correct 52 ms 55164 KB Output is correct
9 Correct 52 ms 55160 KB Output is correct
10 Correct 54 ms 55160 KB Output is correct
11 Correct 52 ms 55160 KB Output is correct
12 Correct 52 ms 55160 KB Output is correct
13 Correct 52 ms 55160 KB Output is correct
14 Correct 52 ms 55160 KB Output is correct
15 Correct 53 ms 55288 KB Output is correct
16 Correct 52 ms 55160 KB Output is correct
17 Correct 53 ms 55160 KB Output is correct
18 Correct 52 ms 55264 KB Output is correct
19 Correct 53 ms 55288 KB Output is correct
20 Correct 52 ms 55160 KB Output is correct
21 Correct 52 ms 55184 KB Output is correct
22 Correct 52 ms 55160 KB Output is correct
23 Correct 52 ms 55288 KB Output is correct
24 Correct 53 ms 55160 KB Output is correct
25 Correct 52 ms 55128 KB Output is correct
26 Correct 55 ms 55544 KB Output is correct
27 Correct 54 ms 55544 KB Output is correct
28 Correct 54 ms 55672 KB Output is correct
29 Correct 55 ms 55572 KB Output is correct
30 Correct 55 ms 55644 KB Output is correct
31 Correct 52 ms 55260 KB Output is correct
32 Correct 54 ms 55804 KB Output is correct
33 Correct 52 ms 55160 KB Output is correct
34 Correct 54 ms 55544 KB Output is correct
35 Correct 58 ms 55700 KB Output is correct
36 Correct 55 ms 55544 KB Output is correct
37 Correct 55 ms 55548 KB Output is correct
38 Correct 53 ms 55160 KB Output is correct
39 Correct 55 ms 55800 KB Output is correct
40 Correct 55 ms 55672 KB Output is correct
41 Correct 55 ms 55544 KB Output is correct
42 Correct 54 ms 55544 KB Output is correct
43 Correct 60 ms 55800 KB Output is correct
44 Correct 53 ms 55232 KB Output is correct
45 Correct 57 ms 55672 KB Output is correct
46 Correct 55 ms 55544 KB Output is correct
47 Correct 52 ms 55160 KB Output is correct
48 Correct 55 ms 55544 KB Output is correct
49 Correct 55 ms 55672 KB Output is correct
50 Correct 54 ms 55672 KB Output is correct
51 Correct 60 ms 55680 KB Output is correct
52 Correct 55 ms 55544 KB Output is correct
53 Correct 55 ms 55544 KB Output is correct
54 Correct 55 ms 55672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 55160 KB Output is correct
2 Correct 52 ms 55132 KB Output is correct
3 Correct 58 ms 55160 KB Output is correct
4 Correct 52 ms 55132 KB Output is correct
5 Correct 60 ms 55160 KB Output is correct
6 Correct 52 ms 55164 KB Output is correct
7 Correct 52 ms 55160 KB Output is correct
8 Correct 52 ms 55164 KB Output is correct
9 Correct 52 ms 55160 KB Output is correct
10 Correct 54 ms 55160 KB Output is correct
11 Correct 52 ms 55160 KB Output is correct
12 Correct 52 ms 55160 KB Output is correct
13 Correct 52 ms 55160 KB Output is correct
14 Correct 52 ms 55160 KB Output is correct
15 Correct 53 ms 55288 KB Output is correct
16 Correct 52 ms 55160 KB Output is correct
17 Correct 53 ms 55160 KB Output is correct
18 Correct 52 ms 55264 KB Output is correct
19 Correct 53 ms 55288 KB Output is correct
20 Correct 52 ms 55160 KB Output is correct
21 Correct 52 ms 55184 KB Output is correct
22 Correct 52 ms 55160 KB Output is correct
23 Correct 52 ms 55288 KB Output is correct
24 Correct 53 ms 55160 KB Output is correct
25 Correct 52 ms 55212 KB Output is correct
26 Correct 174 ms 66400 KB Output is correct
27 Correct 208 ms 71256 KB Output is correct
28 Correct 55 ms 55544 KB Output is correct
29 Correct 62 ms 55172 KB Output is correct
30 Correct 53 ms 55160 KB Output is correct
31 Correct 218 ms 67148 KB Output is correct
32 Correct 55 ms 55544 KB Output is correct
33 Correct 217 ms 72336 KB Output is correct
34 Correct 208 ms 70364 KB Output is correct
35 Correct 56 ms 55548 KB Output is correct
36 Correct 210 ms 66816 KB Output is correct
37 Correct 56 ms 55644 KB Output is correct
38 Correct 56 ms 55544 KB Output is correct
39 Correct 196 ms 66684 KB Output is correct
40 Correct 55 ms 55804 KB Output is correct
41 Correct 195 ms 71192 KB Output is correct
42 Correct 217 ms 72348 KB Output is correct
43 Correct 53 ms 55160 KB Output is correct
44 Correct 210 ms 72768 KB Output is correct
45 Correct 247 ms 72412 KB Output is correct
46 Correct 66 ms 55672 KB Output is correct
47 Correct 66 ms 55672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 176 ms 66780 KB Output is correct
2 Correct 169 ms 68868 KB Output is correct
3 Correct 56 ms 55568 KB Output is correct
4 Correct 56 ms 55672 KB Output is correct
5 Correct 53 ms 55160 KB Output is correct
6 Correct 52 ms 55160 KB Output is correct
7 Correct 66 ms 55544 KB Output is correct
8 Correct 240 ms 67596 KB Output is correct
9 Correct 55 ms 55544 KB Output is correct
10 Correct 198 ms 66784 KB Output is correct
11 Correct 53 ms 55160 KB Output is correct
12 Correct 206 ms 66012 KB Output is correct
13 Correct 212 ms 67220 KB Output is correct
14 Correct 201 ms 68896 KB Output is correct
15 Correct 173 ms 66936 KB Output is correct
16 Correct 56 ms 55548 KB Output is correct
17 Correct 53 ms 55160 KB Output is correct
18 Correct 175 ms 68660 KB Output is correct
19 Correct 222 ms 72148 KB Output is correct
20 Correct 69 ms 55544 KB Output is correct
21 Correct 52 ms 55160 KB Output is correct
22 Correct 179 ms 67804 KB Output is correct
23 Correct 55 ms 55576 KB Output is correct
24 Correct 218 ms 66572 KB Output is correct
25 Correct 206 ms 71004 KB Output is correct
26 Correct 55 ms 55544 KB Output is correct
27 Correct 55 ms 55660 KB Output is correct
28 Correct 55 ms 55548 KB Output is correct
29 Correct 54 ms 55648 KB Output is correct
30 Correct 55 ms 55544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 55160 KB Output is correct
2 Correct 52 ms 55132 KB Output is correct
3 Correct 58 ms 55160 KB Output is correct
4 Correct 52 ms 55132 KB Output is correct
5 Correct 60 ms 55160 KB Output is correct
6 Correct 52 ms 55164 KB Output is correct
7 Correct 52 ms 55160 KB Output is correct
8 Correct 52 ms 55164 KB Output is correct
9 Correct 52 ms 55160 KB Output is correct
10 Correct 54 ms 55160 KB Output is correct
11 Correct 52 ms 55160 KB Output is correct
12 Correct 52 ms 55160 KB Output is correct
13 Correct 52 ms 55160 KB Output is correct
14 Correct 52 ms 55160 KB Output is correct
15 Correct 53 ms 55288 KB Output is correct
16 Correct 52 ms 55160 KB Output is correct
17 Correct 53 ms 55160 KB Output is correct
18 Correct 52 ms 55264 KB Output is correct
19 Correct 53 ms 55288 KB Output is correct
20 Correct 52 ms 55160 KB Output is correct
21 Correct 52 ms 55184 KB Output is correct
22 Correct 52 ms 55160 KB Output is correct
23 Correct 52 ms 55288 KB Output is correct
24 Correct 53 ms 55160 KB Output is correct
25 Correct 52 ms 55128 KB Output is correct
26 Correct 55 ms 55544 KB Output is correct
27 Correct 54 ms 55544 KB Output is correct
28 Correct 54 ms 55672 KB Output is correct
29 Correct 55 ms 55572 KB Output is correct
30 Correct 55 ms 55644 KB Output is correct
31 Correct 52 ms 55260 KB Output is correct
32 Correct 54 ms 55804 KB Output is correct
33 Correct 52 ms 55160 KB Output is correct
34 Correct 54 ms 55544 KB Output is correct
35 Correct 58 ms 55700 KB Output is correct
36 Correct 55 ms 55544 KB Output is correct
37 Correct 55 ms 55548 KB Output is correct
38 Correct 53 ms 55160 KB Output is correct
39 Correct 55 ms 55800 KB Output is correct
40 Correct 55 ms 55672 KB Output is correct
41 Correct 55 ms 55544 KB Output is correct
42 Correct 54 ms 55544 KB Output is correct
43 Correct 60 ms 55800 KB Output is correct
44 Correct 53 ms 55232 KB Output is correct
45 Correct 57 ms 55672 KB Output is correct
46 Correct 55 ms 55544 KB Output is correct
47 Correct 52 ms 55160 KB Output is correct
48 Correct 55 ms 55544 KB Output is correct
49 Correct 55 ms 55672 KB Output is correct
50 Correct 54 ms 55672 KB Output is correct
51 Correct 60 ms 55680 KB Output is correct
52 Correct 55 ms 55544 KB Output is correct
53 Correct 55 ms 55544 KB Output is correct
54 Correct 55 ms 55672 KB Output is correct
55 Correct 52 ms 55212 KB Output is correct
56 Correct 174 ms 66400 KB Output is correct
57 Correct 208 ms 71256 KB Output is correct
58 Correct 55 ms 55544 KB Output is correct
59 Correct 62 ms 55172 KB Output is correct
60 Correct 53 ms 55160 KB Output is correct
61 Correct 218 ms 67148 KB Output is correct
62 Correct 55 ms 55544 KB Output is correct
63 Correct 217 ms 72336 KB Output is correct
64 Correct 208 ms 70364 KB Output is correct
65 Correct 56 ms 55548 KB Output is correct
66 Correct 210 ms 66816 KB Output is correct
67 Correct 56 ms 55644 KB Output is correct
68 Correct 56 ms 55544 KB Output is correct
69 Correct 196 ms 66684 KB Output is correct
70 Correct 55 ms 55804 KB Output is correct
71 Correct 195 ms 71192 KB Output is correct
72 Correct 217 ms 72348 KB Output is correct
73 Correct 53 ms 55160 KB Output is correct
74 Correct 210 ms 72768 KB Output is correct
75 Correct 247 ms 72412 KB Output is correct
76 Correct 66 ms 55672 KB Output is correct
77 Correct 66 ms 55672 KB Output is correct
78 Correct 176 ms 66780 KB Output is correct
79 Correct 169 ms 68868 KB Output is correct
80 Correct 56 ms 55568 KB Output is correct
81 Correct 56 ms 55672 KB Output is correct
82 Correct 53 ms 55160 KB Output is correct
83 Correct 52 ms 55160 KB Output is correct
84 Correct 66 ms 55544 KB Output is correct
85 Correct 240 ms 67596 KB Output is correct
86 Correct 55 ms 55544 KB Output is correct
87 Correct 198 ms 66784 KB Output is correct
88 Correct 53 ms 55160 KB Output is correct
89 Correct 206 ms 66012 KB Output is correct
90 Correct 212 ms 67220 KB Output is correct
91 Correct 201 ms 68896 KB Output is correct
92 Correct 173 ms 66936 KB Output is correct
93 Correct 56 ms 55548 KB Output is correct
94 Correct 53 ms 55160 KB Output is correct
95 Correct 175 ms 68660 KB Output is correct
96 Correct 222 ms 72148 KB Output is correct
97 Correct 69 ms 55544 KB Output is correct
98 Correct 52 ms 55160 KB Output is correct
99 Correct 179 ms 67804 KB Output is correct
100 Correct 55 ms 55576 KB Output is correct
101 Correct 218 ms 66572 KB Output is correct
102 Correct 206 ms 71004 KB Output is correct
103 Correct 55 ms 55544 KB Output is correct
104 Correct 55 ms 55660 KB Output is correct
105 Correct 55 ms 55548 KB Output is correct
106 Correct 54 ms 55648 KB Output is correct
107 Correct 55 ms 55544 KB Output is correct
108 Correct 950 ms 112408 KB Output is correct
109 Correct 1095 ms 136820 KB Output is correct
110 Correct 1077 ms 143656 KB Output is correct
111 Correct 1156 ms 146124 KB Output is correct
112 Correct 1070 ms 136356 KB Output is correct
113 Correct 856 ms 127048 KB Output is correct
114 Correct 1045 ms 133728 KB Output is correct
115 Correct 978 ms 137880 KB Output is correct
116 Correct 1195 ms 120380 KB Output is correct
117 Correct 1012 ms 126920 KB Output is correct
118 Correct 1049 ms 115948 KB Output is correct
119 Correct 1024 ms 126868 KB Output is correct
120 Correct 1219 ms 138432 KB Output is correct
121 Correct 994 ms 126984 KB Output is correct
122 Correct 1154 ms 136860 KB Output is correct
123 Correct 859 ms 128444 KB Output is correct
124 Correct 1121 ms 125384 KB Output is correct