Submission #388657

# Submission time Handle Problem Language Result Execution time Memory
388657 2021-04-12T13:46:41 Z aryan12 Synchronization (JOI13_synchronization) C++17
100 / 100
744 ms 33656 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

struct edge {
	int from, to;
};

const int N = 1e5 + 5;
vector<int> g[N];
int BIT[2 * N];
int tin[N], tout[N];
int tim = 0;
int DP[20][N];
bool isActive[N];

void dfs(int node, int par) {
	DP[0][node] = par;
	tin[node] = ++tim;
	for(int i = 0; i < g[node].size(); i++) {
		if(g[node][i] == par) {
			continue;
		}
		dfs(g[node][i], node);
	}
	tout[node] = ++tim;
}

void Update(int idx, int val) {
	for(int i = idx; i < 2 * N; i += (i & (-i))) {
		BIT[i] += val;
	}
}

int Query(int idx) {
	int ans = 0;
	for(int i = idx; i > 0; i -= (i & (-i))) {
		ans += BIT[i];
	}
	return ans;
}

int FindAncestor(int curNode) {
	int ancestor = curNode;
	int cnt = Query(tin[curNode]);
	for(int i = 19; i >= 0; i--) {
		if(DP[i][ancestor] != -1) {
			int thisCnt = Query(tin[DP[i][ancestor]]);
			if(thisCnt == cnt) {
				ancestor = DP[i][ancestor];
			}
		}
	}
	return ancestor;
}

void Solve() {
	for(int i = 0; i < 2 * N; i++) {
		BIT[i] = 0;
	}
	for(int i = 0; i < N; i++) {
		isActive[i] = false;
	}
	int n, m, q;
	cin >> n >> m >> q;
	vector<edge> edges(n);
	for(int i = 1; i < n; i++) {
		cin >> edges[i].from >> edges[i].to;
		g[edges[i].from].push_back(edges[i].to);
		g[edges[i].to].push_back(edges[i].from);
	}
	int UniqueInfo[n + 1], InfoBeforeDisconnect[n + 1];
	for(int i = 1; i <= n; i++) {
		UniqueInfo[i] = 1;
		InfoBeforeDisconnect[i] = 0;
	}
	dfs(1, -1);
	for(int i = 1; i <= n; i++) {
		Update(tin[i], 1);
		Update(tout[i], -1);
	}
	for(int i = 1; i < 20; i++) {
		for(int j = 1; j <= n; j++) {
			if(DP[i - 1][j] == -1)
				DP[i][j] = -1;
			else
				DP[i][j] = DP[i - 1][DP[i - 1][j]];
		}
	}
	for(int i = 1; i <= m; i++) {
		int edgeIdx;
		cin >> edgeIdx;
		int v = edges[edgeIdx].from, u = edges[edgeIdx].to;
		//cout << "edgeIdx = " << edgeIdx << ", v = " << v << ", u = " << u << "\n";
		if(DP[0][v] != u) {
			swap(v, u);
		}
		//u is the parent
		//cout << "v = " << v << ", u = " << u << ", ancestor = " << FindAncestor(u) << "\n";
		if(!isActive[edgeIdx]) {
			//making it active now
			int ancestor = FindAncestor(u);
			UniqueInfo[ancestor] += (UniqueInfo[v] - InfoBeforeDisconnect[v]);
			UniqueInfo[v] = UniqueInfo[ancestor];
			Update(tin[v], -1);
			Update(tout[v], 1);
		}
		else {
			//making it inactive now
			int ancestor = FindAncestor(v);
			UniqueInfo[v] = UniqueInfo[ancestor];
			InfoBeforeDisconnect[v] = UniqueInfo[v];
			Update(tin[v], 1);
			Update(tout[v], -1);
		}
		isActive[edgeIdx] = !isActive[edgeIdx];
	}
	while(q--) {
		int node;
		cin >> node;
		int ancestor = FindAncestor(node);
		cout << UniqueInfo[ancestor] << "\n";
	}
}

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	Solve();
	return 0;
}

Compilation message

synchronization.cpp: In function 'void dfs(long long int, long long int)':
synchronization.cpp:20:19: 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]
   20 |  for(int i = 0; i < g[node].size(); i++) {
      |                 ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4428 KB Output is correct
2 Correct 3 ms 4428 KB Output is correct
3 Correct 3 ms 4428 KB Output is correct
4 Correct 3 ms 4412 KB Output is correct
5 Correct 3 ms 4428 KB Output is correct
6 Correct 4 ms 4684 KB Output is correct
7 Correct 16 ms 6732 KB Output is correct
8 Correct 16 ms 6860 KB Output is correct
9 Correct 16 ms 6732 KB Output is correct
10 Correct 410 ms 28920 KB Output is correct
11 Correct 341 ms 28868 KB Output is correct
12 Correct 645 ms 32988 KB Output is correct
13 Correct 152 ms 28728 KB Output is correct
14 Correct 223 ms 28528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 30808 KB Output is correct
2 Correct 92 ms 30528 KB Output is correct
3 Correct 125 ms 32500 KB Output is correct
4 Correct 126 ms 32500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4428 KB Output is correct
2 Correct 3 ms 4428 KB Output is correct
3 Correct 3 ms 4468 KB Output is correct
4 Correct 3 ms 4428 KB Output is correct
5 Correct 3 ms 4428 KB Output is correct
6 Correct 4 ms 4684 KB Output is correct
7 Correct 27 ms 7272 KB Output is correct
8 Correct 744 ms 33384 KB Output is correct
9 Correct 712 ms 33368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 726 ms 33348 KB Output is correct
2 Correct 185 ms 33532 KB Output is correct
3 Correct 186 ms 33644 KB Output is correct
4 Correct 189 ms 33572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4428 KB Output is correct
2 Correct 4 ms 4428 KB Output is correct
3 Correct 3 ms 4428 KB Output is correct
4 Correct 4 ms 4428 KB Output is correct
5 Correct 4 ms 4684 KB Output is correct
6 Correct 20 ms 6828 KB Output is correct
7 Correct 406 ms 29380 KB Output is correct
8 Correct 726 ms 33452 KB Output is correct
9 Correct 183 ms 29752 KB Output is correct
10 Correct 240 ms 29644 KB Output is correct
11 Correct 126 ms 31812 KB Output is correct
12 Correct 133 ms 31828 KB Output is correct
13 Correct 188 ms 33656 KB Output is correct