Submission #801689

# Submission time Handle Problem Language Result Execution time Memory
801689 2023-08-02T07:14:24 Z manizare Mergers (JOI19_mergers) C++14
100 / 100
1960 ms 210976 KB
#include <bits/stdc++.h>  
#define pii pair <int,int>  
#define F first  
#define S second  
#define all(a) a.begin(),a.end()  
#define pb push_back 
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 5e5 + 10 , inf  = 1e9 , mod = 1e9 + 7 , lg = 21 ;  
int dp[maxn][lg+1] , d[maxn] , dis[maxn]  , c[maxn] , mark[maxn]  , sm[maxn]  ;
vector <int> G[maxn] , vec[maxn] ; 
map <pii ,int>mp ;

int up(int v ,int d){
  for(int i =0 ; i < lg ; i++){
    if(d>>i&1)v = dp[v][i] ; 
  }
  return v; 
}
int lca(int v ,int u){
  if(dis[v] < dis[u])swap(v ,u) ;
  v = up(v , dis[v] - dis[u]) ;
  for(int i = lg-1 ; i >= 0 ; i--){
    if(dp[v][i] != dp[u][i]){
      v = dp[v][i] ;
      u = dp[u][i] ; 
    }
  }
  if(v == u)return v; 
  return dp[v][0] ;
} 

void dfs1(int v ,int p = 0){
  dp[v][0] = p ;
  for(int i = 1; i < lg ; i++)dp[v][i] = dp[dp[v][i-1]][i-1] ; 
  for(int i= 0; i <G[v].size() ; i++){
    int u = G[v][i] ; 
    if(u == p)continue ; 
    dis[u] = dis[v] + 1 ;
    dfs1(u,v) ; 
  }
}

void dfs2(int v ,int p = 0){
  for(int i =0 ; i < G[v].size() ; i++){
    int u = G[v][i] ; 
    if(u == p)continue ;
    dfs2(u , v) ;
    sm[v]+=sm[u];
  }
  if(sm[v] == 0){
    mp[{v,p}] = mp[{p,v}] = 1; 
  }
}
void dfs3(int v , int p  , int cr ){
  c[v]= cr ;mark[v] = 1; 
  for(int i =0 ; i < G[v].size() ; i++){
    int u = G[v][i] ;
    if(u ==p  || mp[{v,u}] == 1)continue ;
    dfs3(u , v , cr) ; 
  }
}

signed main(){ 
  ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) ;
  int n , k ; cin >> n >> k ; 
  for(int i = 1; i < n ; i++){
    int v ,u  ;
    cin >> v >> u ;
    G[v].pb(u) ;G[u].pb(v) ; 
  }
  dfs1(1) ; 
  for(int i =1 ;i <= n; i++){
    int x ; cin >> x; 
    vec[x].pb(i) ; 
  }
  for(int i = 1; i <= k ; i++){
    int v = vec[i][0] ;
    for(int j = 1 ; j < vec[i].size() ; j++){
      v= lca(v , vec[i][j]) ;  
    }
    for(int j =0 ; j < vec[i].size() ; j++){
      sm[vec[i][j]]++;
      sm[v]--;
    }
  }
  dfs2(1) ;
  int cnt = 1 ; 
  for(int i = 1 ;i <= n ; i++){
    if(mark[i] == 0){
      dfs3(i , 0 , cnt); cnt++ ;
    }
  }
  for(int i = 1; i <= n;  i++){
    for(int j =0 ; j < G[i].size() ; j++){
      int u=  G[i][j] ;
      if(c[u]!=c[i]){
        d[c[u]]++;
      }
    }
  }
  int ans =0 ;
  for(int i = 1; i < cnt ; i++){
    if(d[i]==1)ans++; 
  }
  cout << (ans+1)/2 ;
} 
/* 


*/

Compilation message

mergers.cpp: In function 'void dfs1(int, int)':
mergers.cpp:36:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |   for(int i= 0; i <G[v].size() ; i++){
      |                 ~~^~~~~~~~~~~~
mergers.cpp: In function 'void dfs2(int, int)':
mergers.cpp:45:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |   for(int i =0 ; i < G[v].size() ; i++){
      |                  ~~^~~~~~~~~~~~~
mergers.cpp: In function 'void dfs3(int, int, int)':
mergers.cpp:57:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |   for(int i =0 ; i < G[v].size() ; i++){
      |                  ~~^~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:79:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |     for(int j = 1 ; j < vec[i].size() ; j++){
      |                     ~~^~~~~~~~~~~~~~~
mergers.cpp:82:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     for(int j =0 ; j < vec[i].size() ; j++){
      |                    ~~^~~~~~~~~~~~~~~
mergers.cpp:95:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |     for(int j =0 ; j < G[i].size() ; j++){
      |                    ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23784 KB Output is correct
4 Correct 12 ms 23760 KB Output is correct
5 Correct 11 ms 23764 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 11 ms 23832 KB Output is correct
8 Correct 11 ms 23764 KB Output is correct
9 Correct 11 ms 23764 KB Output is correct
10 Correct 12 ms 23804 KB Output is correct
11 Correct 11 ms 23836 KB Output is correct
12 Correct 12 ms 23820 KB Output is correct
13 Correct 11 ms 23764 KB Output is correct
14 Correct 11 ms 23764 KB Output is correct
15 Correct 14 ms 23772 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23796 KB Output is correct
18 Correct 12 ms 23868 KB Output is correct
19 Correct 11 ms 23784 KB Output is correct
20 Correct 12 ms 23776 KB Output is correct
21 Correct 12 ms 23820 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 13 ms 23764 KB Output is correct
24 Correct 11 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23784 KB Output is correct
4 Correct 12 ms 23760 KB Output is correct
5 Correct 11 ms 23764 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 11 ms 23832 KB Output is correct
8 Correct 11 ms 23764 KB Output is correct
9 Correct 11 ms 23764 KB Output is correct
10 Correct 12 ms 23804 KB Output is correct
11 Correct 11 ms 23836 KB Output is correct
12 Correct 12 ms 23820 KB Output is correct
13 Correct 11 ms 23764 KB Output is correct
14 Correct 11 ms 23764 KB Output is correct
15 Correct 14 ms 23772 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23796 KB Output is correct
18 Correct 12 ms 23868 KB Output is correct
19 Correct 11 ms 23784 KB Output is correct
20 Correct 12 ms 23776 KB Output is correct
21 Correct 12 ms 23820 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 13 ms 23764 KB Output is correct
24 Correct 11 ms 23764 KB Output is correct
25 Correct 11 ms 23764 KB Output is correct
26 Correct 14 ms 24564 KB Output is correct
27 Correct 14 ms 24436 KB Output is correct
28 Correct 14 ms 24752 KB Output is correct
29 Correct 15 ms 24716 KB Output is correct
30 Correct 14 ms 24404 KB Output is correct
31 Correct 12 ms 23764 KB Output is correct
32 Correct 14 ms 24788 KB Output is correct
33 Correct 13 ms 23764 KB Output is correct
34 Correct 13 ms 24432 KB Output is correct
35 Correct 15 ms 24656 KB Output is correct
36 Correct 16 ms 24472 KB Output is correct
37 Correct 14 ms 24472 KB Output is correct
38 Correct 12 ms 23760 KB Output is correct
39 Correct 14 ms 24532 KB Output is correct
40 Correct 14 ms 24404 KB Output is correct
41 Correct 14 ms 24464 KB Output is correct
42 Correct 14 ms 24532 KB Output is correct
43 Correct 14 ms 24720 KB Output is correct
44 Correct 13 ms 23912 KB Output is correct
45 Correct 15 ms 24616 KB Output is correct
46 Correct 14 ms 24532 KB Output is correct
47 Correct 15 ms 23788 KB Output is correct
48 Correct 14 ms 24416 KB Output is correct
49 Correct 14 ms 24792 KB Output is correct
50 Correct 14 ms 24752 KB Output is correct
51 Correct 14 ms 24412 KB Output is correct
52 Correct 13 ms 24448 KB Output is correct
53 Correct 14 ms 24460 KB Output is correct
54 Correct 14 ms 24472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23784 KB Output is correct
4 Correct 12 ms 23760 KB Output is correct
5 Correct 11 ms 23764 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 11 ms 23832 KB Output is correct
8 Correct 11 ms 23764 KB Output is correct
9 Correct 11 ms 23764 KB Output is correct
10 Correct 12 ms 23804 KB Output is correct
11 Correct 11 ms 23836 KB Output is correct
12 Correct 12 ms 23820 KB Output is correct
13 Correct 11 ms 23764 KB Output is correct
14 Correct 11 ms 23764 KB Output is correct
15 Correct 14 ms 23772 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23796 KB Output is correct
18 Correct 12 ms 23868 KB Output is correct
19 Correct 11 ms 23784 KB Output is correct
20 Correct 12 ms 23776 KB Output is correct
21 Correct 12 ms 23820 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 13 ms 23764 KB Output is correct
24 Correct 11 ms 23764 KB Output is correct
25 Correct 12 ms 23820 KB Output is correct
26 Correct 108 ms 45588 KB Output is correct
27 Correct 134 ms 45396 KB Output is correct
28 Correct 14 ms 24404 KB Output is correct
29 Correct 12 ms 23764 KB Output is correct
30 Correct 12 ms 23812 KB Output is correct
31 Correct 120 ms 45268 KB Output is correct
32 Correct 14 ms 24468 KB Output is correct
33 Correct 159 ms 55372 KB Output is correct
34 Correct 119 ms 45384 KB Output is correct
35 Correct 15 ms 24404 KB Output is correct
36 Correct 154 ms 46412 KB Output is correct
37 Correct 14 ms 24460 KB Output is correct
38 Correct 16 ms 24524 KB Output is correct
39 Correct 111 ms 45592 KB Output is correct
40 Correct 15 ms 24788 KB Output is correct
41 Correct 111 ms 45228 KB Output is correct
42 Correct 184 ms 49788 KB Output is correct
43 Correct 13 ms 23820 KB Output is correct
44 Correct 208 ms 55680 KB Output is correct
45 Correct 157 ms 50544 KB Output is correct
46 Correct 14 ms 24516 KB Output is correct
47 Correct 14 ms 24404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 45700 KB Output is correct
2 Correct 174 ms 55220 KB Output is correct
3 Correct 15 ms 24584 KB Output is correct
4 Correct 15 ms 24496 KB Output is correct
5 Correct 14 ms 23756 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 24464 KB Output is correct
8 Correct 166 ms 47464 KB Output is correct
9 Correct 13 ms 24404 KB Output is correct
10 Correct 171 ms 45636 KB Output is correct
11 Correct 10 ms 23816 KB Output is correct
12 Correct 163 ms 45608 KB Output is correct
13 Correct 161 ms 48076 KB Output is correct
14 Correct 172 ms 54992 KB Output is correct
15 Correct 105 ms 45560 KB Output is correct
16 Correct 20 ms 24476 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 176 ms 53664 KB Output is correct
19 Correct 205 ms 60528 KB Output is correct
20 Correct 13 ms 24436 KB Output is correct
21 Correct 14 ms 23764 KB Output is correct
22 Correct 143 ms 48148 KB Output is correct
23 Correct 14 ms 24516 KB Output is correct
24 Correct 180 ms 46472 KB Output is correct
25 Correct 205 ms 58572 KB Output is correct
26 Correct 15 ms 24732 KB Output is correct
27 Correct 15 ms 24864 KB Output is correct
28 Correct 20 ms 24424 KB Output is correct
29 Correct 14 ms 24476 KB Output is correct
30 Correct 16 ms 24472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23784 KB Output is correct
4 Correct 12 ms 23760 KB Output is correct
5 Correct 11 ms 23764 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 11 ms 23832 KB Output is correct
8 Correct 11 ms 23764 KB Output is correct
9 Correct 11 ms 23764 KB Output is correct
10 Correct 12 ms 23804 KB Output is correct
11 Correct 11 ms 23836 KB Output is correct
12 Correct 12 ms 23820 KB Output is correct
13 Correct 11 ms 23764 KB Output is correct
14 Correct 11 ms 23764 KB Output is correct
15 Correct 14 ms 23772 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23796 KB Output is correct
18 Correct 12 ms 23868 KB Output is correct
19 Correct 11 ms 23784 KB Output is correct
20 Correct 12 ms 23776 KB Output is correct
21 Correct 12 ms 23820 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 13 ms 23764 KB Output is correct
24 Correct 11 ms 23764 KB Output is correct
25 Correct 11 ms 23764 KB Output is correct
26 Correct 14 ms 24564 KB Output is correct
27 Correct 14 ms 24436 KB Output is correct
28 Correct 14 ms 24752 KB Output is correct
29 Correct 15 ms 24716 KB Output is correct
30 Correct 14 ms 24404 KB Output is correct
31 Correct 12 ms 23764 KB Output is correct
32 Correct 14 ms 24788 KB Output is correct
33 Correct 13 ms 23764 KB Output is correct
34 Correct 13 ms 24432 KB Output is correct
35 Correct 15 ms 24656 KB Output is correct
36 Correct 16 ms 24472 KB Output is correct
37 Correct 14 ms 24472 KB Output is correct
38 Correct 12 ms 23760 KB Output is correct
39 Correct 14 ms 24532 KB Output is correct
40 Correct 14 ms 24404 KB Output is correct
41 Correct 14 ms 24464 KB Output is correct
42 Correct 14 ms 24532 KB Output is correct
43 Correct 14 ms 24720 KB Output is correct
44 Correct 13 ms 23912 KB Output is correct
45 Correct 15 ms 24616 KB Output is correct
46 Correct 14 ms 24532 KB Output is correct
47 Correct 15 ms 23788 KB Output is correct
48 Correct 14 ms 24416 KB Output is correct
49 Correct 14 ms 24792 KB Output is correct
50 Correct 14 ms 24752 KB Output is correct
51 Correct 14 ms 24412 KB Output is correct
52 Correct 13 ms 24448 KB Output is correct
53 Correct 14 ms 24460 KB Output is correct
54 Correct 14 ms 24472 KB Output is correct
55 Correct 12 ms 23820 KB Output is correct
56 Correct 108 ms 45588 KB Output is correct
57 Correct 134 ms 45396 KB Output is correct
58 Correct 14 ms 24404 KB Output is correct
59 Correct 12 ms 23764 KB Output is correct
60 Correct 12 ms 23812 KB Output is correct
61 Correct 120 ms 45268 KB Output is correct
62 Correct 14 ms 24468 KB Output is correct
63 Correct 159 ms 55372 KB Output is correct
64 Correct 119 ms 45384 KB Output is correct
65 Correct 15 ms 24404 KB Output is correct
66 Correct 154 ms 46412 KB Output is correct
67 Correct 14 ms 24460 KB Output is correct
68 Correct 16 ms 24524 KB Output is correct
69 Correct 111 ms 45592 KB Output is correct
70 Correct 15 ms 24788 KB Output is correct
71 Correct 111 ms 45228 KB Output is correct
72 Correct 184 ms 49788 KB Output is correct
73 Correct 13 ms 23820 KB Output is correct
74 Correct 208 ms 55680 KB Output is correct
75 Correct 157 ms 50544 KB Output is correct
76 Correct 14 ms 24516 KB Output is correct
77 Correct 14 ms 24404 KB Output is correct
78 Correct 106 ms 45700 KB Output is correct
79 Correct 174 ms 55220 KB Output is correct
80 Correct 15 ms 24584 KB Output is correct
81 Correct 15 ms 24496 KB Output is correct
82 Correct 14 ms 23756 KB Output is correct
83 Correct 12 ms 23764 KB Output is correct
84 Correct 12 ms 24464 KB Output is correct
85 Correct 166 ms 47464 KB Output is correct
86 Correct 13 ms 24404 KB Output is correct
87 Correct 171 ms 45636 KB Output is correct
88 Correct 10 ms 23816 KB Output is correct
89 Correct 163 ms 45608 KB Output is correct
90 Correct 161 ms 48076 KB Output is correct
91 Correct 172 ms 54992 KB Output is correct
92 Correct 105 ms 45560 KB Output is correct
93 Correct 20 ms 24476 KB Output is correct
94 Correct 12 ms 23764 KB Output is correct
95 Correct 176 ms 53664 KB Output is correct
96 Correct 205 ms 60528 KB Output is correct
97 Correct 13 ms 24436 KB Output is correct
98 Correct 14 ms 23764 KB Output is correct
99 Correct 143 ms 48148 KB Output is correct
100 Correct 14 ms 24516 KB Output is correct
101 Correct 180 ms 46472 KB Output is correct
102 Correct 205 ms 58572 KB Output is correct
103 Correct 15 ms 24732 KB Output is correct
104 Correct 15 ms 24864 KB Output is correct
105 Correct 20 ms 24424 KB Output is correct
106 Correct 14 ms 24476 KB Output is correct
107 Correct 16 ms 24472 KB Output is correct
108 Correct 963 ms 142876 KB Output is correct
109 Correct 1516 ms 157180 KB Output is correct
110 Correct 1526 ms 172956 KB Output is correct
111 Correct 1644 ms 210976 KB Output is correct
112 Correct 1775 ms 184412 KB Output is correct
113 Correct 1518 ms 174528 KB Output is correct
114 Correct 992 ms 131780 KB Output is correct
115 Correct 1055 ms 131656 KB Output is correct
116 Correct 1104 ms 136584 KB Output is correct
117 Correct 1406 ms 180980 KB Output is correct
118 Correct 1000 ms 131420 KB Output is correct
119 Correct 1690 ms 181004 KB Output is correct
120 Correct 1960 ms 200708 KB Output is correct
121 Correct 1839 ms 180936 KB Output is correct
122 Correct 1553 ms 147228 KB Output is correct
123 Correct 1772 ms 182680 KB Output is correct
124 Correct 1504 ms 166512 KB Output is correct