답안 #700935

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
700935 2023-02-19T12:52:33 Z sunwukong123 수도 (JOI20_capital_city) C++14
100 / 100
359 ms 94900 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{"  << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach 
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first 
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> void chmax(T& a, const T b) { a=max(a,b); }
template <typename T> void chmin(T& a, const T b) { a=min(a,b); }
const int MAXN = 200005;
int n,k;
vector<int> adj[MAXN];
int C[MAXN];
vector<int> V[MAXN];
using namespace std;
int twok[MAXN][20];
int depth[MAXN];
int st[MAXN], en[MAXN], back[MAXN];
const int LOGN=18;
int ctr = 1;
void dfs(int x, int p, int d){
    st[x] = ctr++;
    back[st[x]] = x;
    depth[x] = d;
    twok[x][0] = p;
    for (int i = 1; i <= LOGN; i++){
        if (twok[x][i-1] == -1) break;
        twok[x][i] = twok[twok[x][i - 1]][i - 1]; // eg 16th parent = 8th parent 8th parent
    }
    for (int i = 0; i < adj[x].size(); i++){
        if (adj[x][i] != p) dfs(adj[x][i], x, d + 1);
    }
    en[x] = ++ctr; // changed
    back[en[x]] = x;
}
int kth_parent(int x, int k){
    for (int i = 0; i <= LOGN; i++){
        if (k & (1 << i)) x = twok[x][i];
        if (x <= -1) return -1;
    }
    return x;
}
int lca(int x, int y){
    if (depth[x] > depth[y]) {
        int dd = depth[x] - depth[y];
        x = kth_parent(x, dd);
    } else {
        int dd = depth[y] - depth[x];
        y = kth_parent(y, dd);
    }
    if (x == y) return x;
    
    for (int i = 17; i >= 0; i--){
        if (twok[x][i] != twok[y][i]){
            x = twok[x][i];
            y = twok[y][i];
        }
    }
    return twok[x][0];
}
int LCA[MAXN];
namespace tarjan {
	int num[MAXN], low[MAXN], weight[MAXN], whichScc[MAXN];
	bool instack[MAXN];
	vector<int> adjlist[MAXN];
	vector<int> sccadjlist[MAXN];
	stack<int> nodes;
	int ctr = 0;
	int sccptr = 0;
	void dfs(int x){
	    low[x] = num[x] = ctr++;
	    nodes.push(x);
	    instack[x] = true;
	    for (auto i : adjlist[x]){
	        if (num[i] == -1) dfs(i);
	        if (instack[i]) low[x] = min(low[i], low[x]);
	    }
	    
	    if (low[x] == num[x]){
	        // loop through the nodes of the found SCC.
	        while (instack[x]){
	            int v = nodes.top(); nodes.pop();
	            instack[v] = false;
	            whichScc[v] = sccptr;
	            weight[sccptr]++;
	        }
	        sccptr++;
	    }
	}
}
int32_t main() 
{
	IAMSPEED
	cin >> n >> k;
	FOR(i,1,n-1) {
		int a,b; cin >> a >> b;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	FOR(i,1,n) cin >> C[i];
	FOR(i,1,n) {
		V[C[i]].pb(i);
	}
	memset(twok,-1,sizeof twok);
	dfs(1,-1,0);
	FOR(i,1,k) {
		sort(all(V[i]),[](int x, int y) {
			return st[x]<st[y];
		});
	}
	FOR(i,1,k) {
		LCA[i]=lca(V[i][0],V[i].back());
	}
	FOR(i,1,n) {
		if(i == LCA[C[i]]){

		} else {
			if(twok[i][0] != -1) {
				if(C[i] != C[twok[i][0]])tarjan::adjlist[C[i]].pb(C[twok[i][0]]);
			}
		}
	}
	memset(tarjan::num,-1,sizeof tarjan::num);
	FOR(i,1,k) {
		if(tarjan::num[i] == -1) tarjan::dfs(i);
	}
	FOR(i,1,k) {
		for(auto j:tarjan::adjlist[i]) {
			if(tarjan::whichScc[i] != tarjan::whichScc[j]) {
				tarjan::sccadjlist[tarjan::whichScc[i]].pb(tarjan::whichScc[j]);
			}
		}
	}

	int ans = inf;
	FOR(i,0,tarjan::sccptr-1) {
		if(tarjan::sccadjlist[i].size() == 0){
			chmin(ans, tarjan::weight[i]);
		}
	}
	cout << --ans;

	return (0-0) ; 
}



Compilation message

capital_city.cpp: In function 'void dfs(long long int, long long int, long long int)':
capital_city.cpp:63:23: 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]
   63 |     for (int i = 0; i < adj[x].size(); i++){
      |                     ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 52052 KB Output is correct
2 Correct 21 ms 51960 KB Output is correct
3 Correct 22 ms 52020 KB Output is correct
4 Correct 25 ms 52060 KB Output is correct
5 Correct 23 ms 51940 KB Output is correct
6 Correct 22 ms 51988 KB Output is correct
7 Correct 21 ms 51944 KB Output is correct
8 Correct 21 ms 52052 KB Output is correct
9 Correct 24 ms 52028 KB Output is correct
10 Correct 25 ms 51992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 52052 KB Output is correct
2 Correct 21 ms 51960 KB Output is correct
3 Correct 22 ms 52020 KB Output is correct
4 Correct 25 ms 52060 KB Output is correct
5 Correct 23 ms 51940 KB Output is correct
6 Correct 22 ms 51988 KB Output is correct
7 Correct 21 ms 51944 KB Output is correct
8 Correct 21 ms 52052 KB Output is correct
9 Correct 24 ms 52028 KB Output is correct
10 Correct 25 ms 51992 KB Output is correct
11 Correct 27 ms 52312 KB Output is correct
12 Correct 23 ms 52296 KB Output is correct
13 Correct 25 ms 52324 KB Output is correct
14 Correct 22 ms 52236 KB Output is correct
15 Correct 23 ms 52276 KB Output is correct
16 Correct 22 ms 52352 KB Output is correct
17 Correct 25 ms 52324 KB Output is correct
18 Correct 26 ms 52336 KB Output is correct
19 Correct 23 ms 52308 KB Output is correct
20 Correct 24 ms 52364 KB Output is correct
21 Correct 23 ms 52288 KB Output is correct
22 Correct 27 ms 52424 KB Output is correct
23 Correct 23 ms 52424 KB Output is correct
24 Correct 26 ms 52300 KB Output is correct
25 Correct 22 ms 52352 KB Output is correct
26 Correct 23 ms 52436 KB Output is correct
27 Correct 23 ms 52296 KB Output is correct
28 Correct 25 ms 52296 KB Output is correct
29 Correct 22 ms 52372 KB Output is correct
30 Correct 23 ms 52308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 329 ms 94452 KB Output is correct
2 Correct 145 ms 94900 KB Output is correct
3 Correct 337 ms 94136 KB Output is correct
4 Correct 136 ms 94784 KB Output is correct
5 Correct 307 ms 91252 KB Output is correct
6 Correct 151 ms 94796 KB Output is correct
7 Correct 289 ms 90872 KB Output is correct
8 Correct 143 ms 92868 KB Output is correct
9 Correct 245 ms 85172 KB Output is correct
10 Correct 244 ms 83096 KB Output is correct
11 Correct 269 ms 85568 KB Output is correct
12 Correct 328 ms 87712 KB Output is correct
13 Correct 241 ms 82960 KB Output is correct
14 Correct 267 ms 88004 KB Output is correct
15 Correct 232 ms 87844 KB Output is correct
16 Correct 226 ms 83632 KB Output is correct
17 Correct 230 ms 84492 KB Output is correct
18 Correct 234 ms 84728 KB Output is correct
19 Correct 237 ms 87132 KB Output is correct
20 Correct 237 ms 88628 KB Output is correct
21 Correct 22 ms 52052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 52052 KB Output is correct
2 Correct 21 ms 51960 KB Output is correct
3 Correct 22 ms 52020 KB Output is correct
4 Correct 25 ms 52060 KB Output is correct
5 Correct 23 ms 51940 KB Output is correct
6 Correct 22 ms 51988 KB Output is correct
7 Correct 21 ms 51944 KB Output is correct
8 Correct 21 ms 52052 KB Output is correct
9 Correct 24 ms 52028 KB Output is correct
10 Correct 25 ms 51992 KB Output is correct
11 Correct 27 ms 52312 KB Output is correct
12 Correct 23 ms 52296 KB Output is correct
13 Correct 25 ms 52324 KB Output is correct
14 Correct 22 ms 52236 KB Output is correct
15 Correct 23 ms 52276 KB Output is correct
16 Correct 22 ms 52352 KB Output is correct
17 Correct 25 ms 52324 KB Output is correct
18 Correct 26 ms 52336 KB Output is correct
19 Correct 23 ms 52308 KB Output is correct
20 Correct 24 ms 52364 KB Output is correct
21 Correct 23 ms 52288 KB Output is correct
22 Correct 27 ms 52424 KB Output is correct
23 Correct 23 ms 52424 KB Output is correct
24 Correct 26 ms 52300 KB Output is correct
25 Correct 22 ms 52352 KB Output is correct
26 Correct 23 ms 52436 KB Output is correct
27 Correct 23 ms 52296 KB Output is correct
28 Correct 25 ms 52296 KB Output is correct
29 Correct 22 ms 52372 KB Output is correct
30 Correct 23 ms 52308 KB Output is correct
31 Correct 329 ms 94452 KB Output is correct
32 Correct 145 ms 94900 KB Output is correct
33 Correct 337 ms 94136 KB Output is correct
34 Correct 136 ms 94784 KB Output is correct
35 Correct 307 ms 91252 KB Output is correct
36 Correct 151 ms 94796 KB Output is correct
37 Correct 289 ms 90872 KB Output is correct
38 Correct 143 ms 92868 KB Output is correct
39 Correct 245 ms 85172 KB Output is correct
40 Correct 244 ms 83096 KB Output is correct
41 Correct 269 ms 85568 KB Output is correct
42 Correct 328 ms 87712 KB Output is correct
43 Correct 241 ms 82960 KB Output is correct
44 Correct 267 ms 88004 KB Output is correct
45 Correct 232 ms 87844 KB Output is correct
46 Correct 226 ms 83632 KB Output is correct
47 Correct 230 ms 84492 KB Output is correct
48 Correct 234 ms 84728 KB Output is correct
49 Correct 237 ms 87132 KB Output is correct
50 Correct 237 ms 88628 KB Output is correct
51 Correct 22 ms 52052 KB Output is correct
52 Correct 200 ms 77900 KB Output is correct
53 Correct 206 ms 77796 KB Output is correct
54 Correct 201 ms 77796 KB Output is correct
55 Correct 219 ms 77972 KB Output is correct
56 Correct 210 ms 77872 KB Output is correct
57 Correct 213 ms 77820 KB Output is correct
58 Correct 286 ms 83252 KB Output is correct
59 Correct 279 ms 83820 KB Output is correct
60 Correct 341 ms 83660 KB Output is correct
61 Correct 359 ms 83316 KB Output is correct
62 Correct 146 ms 94836 KB Output is correct
63 Correct 137 ms 94856 KB Output is correct
64 Correct 134 ms 93392 KB Output is correct
65 Correct 141 ms 94668 KB Output is correct
66 Correct 207 ms 83924 KB Output is correct
67 Correct 231 ms 83772 KB Output is correct
68 Correct 216 ms 83896 KB Output is correct
69 Correct 217 ms 83912 KB Output is correct
70 Correct 224 ms 83848 KB Output is correct
71 Correct 211 ms 83904 KB Output is correct
72 Correct 225 ms 83780 KB Output is correct
73 Correct 225 ms 83164 KB Output is correct
74 Correct 227 ms 83860 KB Output is correct
75 Correct 279 ms 83856 KB Output is correct
76 Correct 310 ms 88524 KB Output is correct
77 Correct 326 ms 86992 KB Output is correct
78 Correct 248 ms 84284 KB Output is correct
79 Correct 243 ms 82432 KB Output is correct
80 Correct 248 ms 88112 KB Output is correct
81 Correct 264 ms 85304 KB Output is correct
82 Correct 232 ms 85472 KB Output is correct
83 Correct 251 ms 82800 KB Output is correct
84 Correct 238 ms 87592 KB Output is correct
85 Correct 230 ms 86180 KB Output is correct
86 Correct 227 ms 82524 KB Output is correct
87 Correct 230 ms 83848 KB Output is correct
88 Correct 268 ms 86204 KB Output is correct
89 Correct 275 ms 82916 KB Output is correct
90 Correct 283 ms 82600 KB Output is correct
91 Correct 283 ms 84592 KB Output is correct
92 Correct 304 ms 83988 KB Output is correct
93 Correct 264 ms 83268 KB Output is correct
94 Correct 273 ms 82900 KB Output is correct
95 Correct 310 ms 84048 KB Output is correct
96 Correct 260 ms 83128 KB Output is correct
97 Correct 273 ms 84796 KB Output is correct