Submission #925682

# Submission time Handle Problem Language Result Execution time Memory
925682 2024-02-12T07:24:31 Z cig32 Capital City (JOI20_capital_city) C++17
11 / 100
3000 ms 72324 KB
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define double long double
const int MAXN = 5e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
  if(p==0) return 1 % MOD;
  int r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
int inv(int b) { 
  return bm(b, MOD-2);
}
int fastlog(int x) {
  return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
static void run_with_stack_size(void (*func)(void), size_t stsize) {
  char *stack, *send;
  stack = (char *)malloc(stsize);
  send = stack + stsize - 16;
  send = (char *)((uintptr_t)send / 16 * 16);
  asm volatile(
    "mov %%rsp, (%0)\n"
    "mov %0, %%rsp\n"
    :
    : "r"(send));
  func();
  asm volatile("mov (%0), %%rsp\n" : : "r"(send));
  free(stack);
}
int n, k;
int c[MAXN];
vector<int> tr[MAXN];
vector<int> adj[MAXN], rev[MAXN];
bool have(int node, int prv, int tar) {
  if(c[node] == tar) return 1;
  for(int x: tr[node]) {
    if(x != prv) {
      if(have(x, node, tar)) return 1;
    }
  }
  return 0;
}
stack<int> ts;
bool vis[MAXN];
void dfs1(int node) {
  vis[node] = 1;
  for(int x: adj[node]) {
    if(!vis[x]) dfs1(x);
  }
  ts.push(node);
}
vector<int> comp;
vector<int> cond[MAXN];
int dsu[MAXN];
int set_of(int u) {
  if(dsu[u] == u) return u;
  return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
  dsu[set_of(u)] = set_of(v);
}
void dfs2(int node) {
  vis[node] = 1;
  for(int x: rev[node]) {
    if(!vis[x]) {
      dfs2(x);
    }
  }
  comp.push_back(node);
}
void solve(int tc) { 
  cin >> n >> k;
  vector<pair<int,int> > edges;
  for(int i=1; i<n; i++) {
    int u, v;
    cin >> u >> v;
    edges.push_back({u, v});
    tr[u].push_back(v);
    tr[v].push_back(u);
  }
  for(int i=1; i<=n; i++) {
    cin >> c[i];
  }
  for(pair<int, int> k : edges) {
    if(have(k.first, k.second, c[k.second])) {
      adj[c[k.second]].push_back(c[k.first]);
    }
    if(have(k.second, k.first, c[k.first])) {
      adj[c[k.first]].push_back(c[k.second]);
    }
  }
  for(int i=1; i<=k; i++) {
    for(int x: adj[i]) {
      rev[x].push_back(i);
    }
  }
  for(int i=1; i<=k; i++) {
    if(!vis[i]) {
      dfs1(i);
    }
  }
  for(int i=1; i<=k; i++) vis[i] = 0, dsu[i] = i;
  while(ts.size()) {
    int tp = ts.top();
    ts.pop();
    if(!vis[tp]) {
      comp.clear();
      dfs2(tp);
      for(int i=1; i<comp.size(); i++) {
        union_(comp[i-1], comp[i]);
      }
    }
  }
  set<pair<int,int>> newedges;
  for(int i=1; i<=k; i++) {
    for(int x: adj[i]) {
      if(set_of(i) != set_of(x) && !newedges.count({set_of(i), set_of(x)})) {
        cond[set_of(i)].push_back(set_of(x));
        newedges.insert({set_of(i), set_of(x)});
      }
    }
  }
  int ans = 1e9;
  int cnt[k+1];
  for(int i=1; i<=k; i++) {
    cnt[i] = 0;
  }
  for(int i=1; i<=k; i++) {
    cnt[set_of(i)]++;
  }

  for(int i=1; i<=k; i++) {
    if(set_of(i) == i && cond[i].empty()) {
      ans = min(ans, cnt[i]);
    }
  }
  cout << ans - 1 << "\n";
}

void uwu() {
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
int32_t main() {
  #ifdef ONLINE_JUDGE
  uwu();
  #endif
  #ifndef ONLINE_JUDGE
  run_with_stack_size(uwu, 1024 * 1024 * 1024); // run with a 1 GiB stack
  #endif
}
/*
g++ A.cpp -std=c++17 -O2 -o A
./A < input.txt
*/

Compilation message

capital_city.cpp: In function 'void solve(long long int)':
capital_city.cpp:117:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |       for(int i=1; i<comp.size(); i++) {
      |                    ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 49500 KB Output is correct
2 Correct 12 ms 49736 KB Output is correct
3 Correct 11 ms 49744 KB Output is correct
4 Correct 11 ms 49736 KB Output is correct
5 Correct 10 ms 49496 KB Output is correct
6 Correct 11 ms 49500 KB Output is correct
7 Correct 11 ms 49496 KB Output is correct
8 Correct 11 ms 49752 KB Output is correct
9 Correct 10 ms 49496 KB Output is correct
10 Correct 11 ms 49500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 49500 KB Output is correct
2 Correct 12 ms 49736 KB Output is correct
3 Correct 11 ms 49744 KB Output is correct
4 Correct 11 ms 49736 KB Output is correct
5 Correct 10 ms 49496 KB Output is correct
6 Correct 11 ms 49500 KB Output is correct
7 Correct 11 ms 49496 KB Output is correct
8 Correct 11 ms 49752 KB Output is correct
9 Correct 10 ms 49496 KB Output is correct
10 Correct 11 ms 49500 KB Output is correct
11 Correct 16 ms 49756 KB Output is correct
12 Correct 17 ms 49756 KB Output is correct
13 Correct 17 ms 49752 KB Output is correct
14 Correct 16 ms 49756 KB Output is correct
15 Correct 26 ms 50000 KB Output is correct
16 Correct 26 ms 50004 KB Output is correct
17 Correct 20 ms 50012 KB Output is correct
18 Correct 24 ms 50360 KB Output is correct
19 Correct 21 ms 50012 KB Output is correct
20 Correct 20 ms 50012 KB Output is correct
21 Correct 20 ms 50012 KB Output is correct
22 Correct 26 ms 50012 KB Output is correct
23 Correct 31 ms 50008 KB Output is correct
24 Correct 27 ms 50012 KB Output is correct
25 Correct 24 ms 49944 KB Output is correct
26 Correct 23 ms 49960 KB Output is correct
27 Correct 25 ms 50012 KB Output is correct
28 Correct 22 ms 50012 KB Output is correct
29 Correct 22 ms 49956 KB Output is correct
30 Correct 23 ms 49944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3018 ms 72324 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 49500 KB Output is correct
2 Correct 12 ms 49736 KB Output is correct
3 Correct 11 ms 49744 KB Output is correct
4 Correct 11 ms 49736 KB Output is correct
5 Correct 10 ms 49496 KB Output is correct
6 Correct 11 ms 49500 KB Output is correct
7 Correct 11 ms 49496 KB Output is correct
8 Correct 11 ms 49752 KB Output is correct
9 Correct 10 ms 49496 KB Output is correct
10 Correct 11 ms 49500 KB Output is correct
11 Correct 16 ms 49756 KB Output is correct
12 Correct 17 ms 49756 KB Output is correct
13 Correct 17 ms 49752 KB Output is correct
14 Correct 16 ms 49756 KB Output is correct
15 Correct 26 ms 50000 KB Output is correct
16 Correct 26 ms 50004 KB Output is correct
17 Correct 20 ms 50012 KB Output is correct
18 Correct 24 ms 50360 KB Output is correct
19 Correct 21 ms 50012 KB Output is correct
20 Correct 20 ms 50012 KB Output is correct
21 Correct 20 ms 50012 KB Output is correct
22 Correct 26 ms 50012 KB Output is correct
23 Correct 31 ms 50008 KB Output is correct
24 Correct 27 ms 50012 KB Output is correct
25 Correct 24 ms 49944 KB Output is correct
26 Correct 23 ms 49960 KB Output is correct
27 Correct 25 ms 50012 KB Output is correct
28 Correct 22 ms 50012 KB Output is correct
29 Correct 22 ms 49956 KB Output is correct
30 Correct 23 ms 49944 KB Output is correct
31 Execution timed out 3018 ms 72324 KB Time limit exceeded
32 Halted 0 ms 0 KB -