Submission #209516

# Submission time Handle Problem Language Result Execution time Memory
209516 2020-03-14T12:32:13 Z mieszko11b Bridges (APIO19_bridges) C++14
73 / 100
3000 ms 25536 KB
#include <bits/stdc++.h>

using namespace std;

using ii = pair<int, int>;

#define X first
#define Y second

const int SQRT = 600;

int n, m;
int a[100007], b[100007], c[100007];
int q;
int t[100007], d[100007], e[100007], ans[100007];
bool changed[100007];
int lastw[100007];
int maxv;
vector<int> not_changed[200007];
vector<int> G[50007];

void readint(int &x) {
	x = 0;
	char c;
	do
		c = getchar_unlocked();
	while(c < '0' || c > '9');
	
	do {
		x = x * 10 + c - '0';
		c = getchar_unlocked();
	} while(c >= '0' && c <= '9');
}

int p[50007], ran[50007];
bool vis[50007];

int cnt;
vector<int> aff;

void init_DSU() {
	for(int i = 1 ; i <= n ; i++) {
		p[i] = i;
		ran[i] = 1;
	}
}

int Find(int i) {
	if(p[i] == i)
		return i;
	return p[i] = Find(p[i]);
}

void Union(int a, int b) {
	a = Find(a);
	b = Find(b);
	
	if(a == b)
		return ;
		
	if(ran[a] < ran[b]) {
		p[a] = b;
		ran[b] += ran[a];
	} else {
		p[b] = a;
		ran[a] += ran[b];
	}
}

void dfs(int w) {
	if(vis[w])
		return ;
	vis[w] = 1;
	aff.push_back(w);
	cnt += ran[w];
	for(int u : G[w])
		dfs(u);
}

int count(vector<ii> &E, int w) {
	for(ii e : E) {
		G[e.X].push_back(e.Y);
		G[e.Y].push_back(e.X);
	}
	cnt = 0;
	dfs(Find(w));
	for(int x : aff)
		vis[x] = 0;
	aff.clear();
	for(ii e : E) {
		G[e.X].clear();
		G[e.Y].clear();
	}
	return cnt;
}


void process_queries(int x, int y) {
	vector<ii> Q;
	vector<int> chacha;
	chacha.reserve(y - x + 1);
	Q.reserve(y - x + 1);
	memset(changed, 0, sizeof changed);
	for(int i = x ; i <= y ; i++) {
		if(t[i] == 1)
			changed[d[i]] = 1;
		else
			Q.emplace_back(e[i], i);
	}
	for(int j = 0 ; j < maxv ; j++)
		not_changed[j].clear();
	for(int i = 1 ; i <= m ; i++) {
		if(!changed[i])
			not_changed[c[i]].push_back(i);
		else
			chacha.push_back(i);
	}
	sort(Q.begin(), Q.end(), greater<ii>());
	
	init_DSU();
	
	int v = maxv;
	for(int j = 0 ; j < Q.size() ; j++) {
		int q = Q[j].Y;
		
		for(int i = e[q] ; i < v ; i++)
			for(int e : not_changed[i])
				Union(a[e], b[e]);
		v = e[q];
				
		for(int e : chacha)
			lastw[e] = c[e];
		for(int k = x ; k < q ; k++)
			if(t[k] == 1)
				lastw[d[k]] = e[k];
		vector<ii> E;
		E.reserve(y - x + 1);
		for(int e : chacha)
			if(lastw[e] >= ::e[q])
				E.push_back({Find(a[e]), Find(b[e])});
		ans[q] = count(E, d[q]);
	}

	for(int k = x ; k <= y ; k++)
		if(t[k] == 1)
			c[d[k]] = e[k];	
}

int main() {
	//~ scanf("%d%d",&n, &m);
	readint(n);
	readint(m);
	
	vector<int> hlp;
	
	for(int i = 1 ; i <= m ; i++) {
		//~ scanf("%d%d%d", &a[i], &b[i], &c[i]);
		readint(a[i]);
		readint(b[i]);
		readint(c[i]);
		hlp.push_back(c[i]);
	}
	//~ scanf("%d", &q);
	readint(q);
	for(int i = 1 ; i <= q ; i++) {
		//~ scanf("%d%d%d", &t[i], &d[i], &e[i]);
		readint(t[i]);
		readint(d[i]);
		readint(e[i]);
		hlp.push_back(e[i]);
	}
		
	sort(hlp.begin(), hlp.end());
	hlp.resize(distance(hlp.begin(), unique(hlp.begin(), hlp.end())));
	map<int, int> M;
	for(int i = 0 ; i < hlp.size() ; i++)
		M[hlp[i]] = i;
	maxv = hlp.size();
	for(int i = 1 ; i <= m ; i++)
		c[i] = M[c[i]];
	for(int i = 1 ; i <= q ; i++)
		e[i] = M[e[i]];
		
	for(int i = 1 ; i <= q ; i += SQRT)
		process_queries(i, min(i + SQRT - 1, q));
		
	for(int i = 1 ; i <= q ; i++)
		if(ans[i])
			printf("%d\n", ans[i]);
		
	return 0;
}

Compilation message

bridges.cpp: In function 'void process_queries(int, int)':
bridges.cpp:123:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int j = 0 ; j < Q.size() ; j++) {
                  ~~^~~~~~~~~~
bridges.cpp: In function 'int main()':
bridges.cpp:176:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < hlp.size() ; i++)
                  ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6264 KB Output is correct
2 Correct 8 ms 6264 KB Output is correct
3 Correct 49 ms 7288 KB Output is correct
4 Correct 17 ms 7032 KB Output is correct
5 Correct 28 ms 6904 KB Output is correct
6 Correct 25 ms 6776 KB Output is correct
7 Correct 25 ms 6648 KB Output is correct
8 Correct 33 ms 7020 KB Output is correct
9 Correct 31 ms 6648 KB Output is correct
10 Correct 27 ms 7032 KB Output is correct
11 Correct 26 ms 7032 KB Output is correct
12 Correct 29 ms 7032 KB Output is correct
13 Correct 33 ms 7036 KB Output is correct
14 Correct 31 ms 7032 KB Output is correct
15 Correct 29 ms 7032 KB Output is correct
16 Correct 26 ms 6648 KB Output is correct
17 Correct 26 ms 6648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1956 ms 21444 KB Output is correct
2 Correct 1814 ms 21220 KB Output is correct
3 Correct 1829 ms 21224 KB Output is correct
4 Correct 1741 ms 21224 KB Output is correct
5 Correct 1746 ms 21200 KB Output is correct
6 Correct 2063 ms 19396 KB Output is correct
7 Correct 1974 ms 19900 KB Output is correct
8 Correct 1901 ms 20076 KB Output is correct
9 Correct 151 ms 13168 KB Output is correct
10 Correct 995 ms 11232 KB Output is correct
11 Correct 912 ms 11372 KB Output is correct
12 Correct 1241 ms 18540 KB Output is correct
13 Correct 1587 ms 21608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1361 ms 19228 KB Output is correct
2 Correct 818 ms 14752 KB Output is correct
3 Correct 1432 ms 18028 KB Output is correct
4 Correct 1511 ms 19052 KB Output is correct
5 Correct 154 ms 13168 KB Output is correct
6 Correct 1401 ms 19112 KB Output is correct
7 Correct 1306 ms 18976 KB Output is correct
8 Correct 1158 ms 18920 KB Output is correct
9 Correct 959 ms 17512 KB Output is correct
10 Correct 916 ms 17388 KB Output is correct
11 Correct 1104 ms 20204 KB Output is correct
12 Correct 1018 ms 20336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2796 ms 23244 KB Output is correct
2 Correct 154 ms 13168 KB Output is correct
3 Correct 247 ms 16368 KB Output is correct
4 Correct 51 ms 8944 KB Output is correct
5 Correct 651 ms 11496 KB Output is correct
6 Correct 2896 ms 23148 KB Output is correct
7 Correct 629 ms 11372 KB Output is correct
8 Correct 1311 ms 18452 KB Output is correct
9 Correct 1199 ms 18284 KB Output is correct
10 Correct 1046 ms 18412 KB Output is correct
11 Correct 2072 ms 20716 KB Output is correct
12 Correct 1971 ms 20716 KB Output is correct
13 Correct 1638 ms 20844 KB Output is correct
14 Correct 619 ms 11632 KB Output is correct
15 Correct 625 ms 11624 KB Output is correct
16 Correct 2757 ms 23148 KB Output is correct
17 Correct 2692 ms 23148 KB Output is correct
18 Correct 2700 ms 23136 KB Output is correct
19 Correct 2802 ms 23276 KB Output is correct
20 Correct 1917 ms 21648 KB Output is correct
21 Correct 1932 ms 21628 KB Output is correct
22 Correct 2402 ms 22888 KB Output is correct
23 Correct 718 ms 10988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1956 ms 21444 KB Output is correct
2 Correct 1814 ms 21220 KB Output is correct
3 Correct 1829 ms 21224 KB Output is correct
4 Correct 1741 ms 21224 KB Output is correct
5 Correct 1746 ms 21200 KB Output is correct
6 Correct 2063 ms 19396 KB Output is correct
7 Correct 1974 ms 19900 KB Output is correct
8 Correct 1901 ms 20076 KB Output is correct
9 Correct 151 ms 13168 KB Output is correct
10 Correct 995 ms 11232 KB Output is correct
11 Correct 912 ms 11372 KB Output is correct
12 Correct 1241 ms 18540 KB Output is correct
13 Correct 1587 ms 21608 KB Output is correct
14 Correct 1361 ms 19228 KB Output is correct
15 Correct 818 ms 14752 KB Output is correct
16 Correct 1432 ms 18028 KB Output is correct
17 Correct 1511 ms 19052 KB Output is correct
18 Correct 154 ms 13168 KB Output is correct
19 Correct 1401 ms 19112 KB Output is correct
20 Correct 1306 ms 18976 KB Output is correct
21 Correct 1158 ms 18920 KB Output is correct
22 Correct 959 ms 17512 KB Output is correct
23 Correct 916 ms 17388 KB Output is correct
24 Correct 1104 ms 20204 KB Output is correct
25 Correct 1018 ms 20336 KB Output is correct
26 Correct 1933 ms 21224 KB Output is correct
27 Correct 2148 ms 20128 KB Output is correct
28 Correct 1925 ms 21228 KB Output is correct
29 Correct 1648 ms 21224 KB Output is correct
30 Correct 2099 ms 20644 KB Output is correct
31 Correct 2185 ms 20540 KB Output is correct
32 Correct 2167 ms 20584 KB Output is correct
33 Correct 2009 ms 21228 KB Output is correct
34 Correct 2029 ms 21228 KB Output is correct
35 Correct 2049 ms 21228 KB Output is correct
36 Correct 1705 ms 21228 KB Output is correct
37 Correct 1679 ms 21228 KB Output is correct
38 Correct 1751 ms 21228 KB Output is correct
39 Correct 1349 ms 19384 KB Output is correct
40 Correct 1312 ms 19308 KB Output is correct
41 Correct 1285 ms 19304 KB Output is correct
42 Correct 1577 ms 22508 KB Output is correct
43 Correct 1476 ms 22652 KB Output is correct
44 Correct 1547 ms 22536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6264 KB Output is correct
2 Correct 8 ms 6264 KB Output is correct
3 Correct 49 ms 7288 KB Output is correct
4 Correct 17 ms 7032 KB Output is correct
5 Correct 28 ms 6904 KB Output is correct
6 Correct 25 ms 6776 KB Output is correct
7 Correct 25 ms 6648 KB Output is correct
8 Correct 33 ms 7020 KB Output is correct
9 Correct 31 ms 6648 KB Output is correct
10 Correct 27 ms 7032 KB Output is correct
11 Correct 26 ms 7032 KB Output is correct
12 Correct 29 ms 7032 KB Output is correct
13 Correct 33 ms 7036 KB Output is correct
14 Correct 31 ms 7032 KB Output is correct
15 Correct 29 ms 7032 KB Output is correct
16 Correct 26 ms 6648 KB Output is correct
17 Correct 26 ms 6648 KB Output is correct
18 Correct 1956 ms 21444 KB Output is correct
19 Correct 1814 ms 21220 KB Output is correct
20 Correct 1829 ms 21224 KB Output is correct
21 Correct 1741 ms 21224 KB Output is correct
22 Correct 1746 ms 21200 KB Output is correct
23 Correct 2063 ms 19396 KB Output is correct
24 Correct 1974 ms 19900 KB Output is correct
25 Correct 1901 ms 20076 KB Output is correct
26 Correct 151 ms 13168 KB Output is correct
27 Correct 995 ms 11232 KB Output is correct
28 Correct 912 ms 11372 KB Output is correct
29 Correct 1241 ms 18540 KB Output is correct
30 Correct 1587 ms 21608 KB Output is correct
31 Correct 1361 ms 19228 KB Output is correct
32 Correct 818 ms 14752 KB Output is correct
33 Correct 1432 ms 18028 KB Output is correct
34 Correct 1511 ms 19052 KB Output is correct
35 Correct 154 ms 13168 KB Output is correct
36 Correct 1401 ms 19112 KB Output is correct
37 Correct 1306 ms 18976 KB Output is correct
38 Correct 1158 ms 18920 KB Output is correct
39 Correct 959 ms 17512 KB Output is correct
40 Correct 916 ms 17388 KB Output is correct
41 Correct 1104 ms 20204 KB Output is correct
42 Correct 1018 ms 20336 KB Output is correct
43 Correct 2796 ms 23244 KB Output is correct
44 Correct 154 ms 13168 KB Output is correct
45 Correct 247 ms 16368 KB Output is correct
46 Correct 51 ms 8944 KB Output is correct
47 Correct 651 ms 11496 KB Output is correct
48 Correct 2896 ms 23148 KB Output is correct
49 Correct 629 ms 11372 KB Output is correct
50 Correct 1311 ms 18452 KB Output is correct
51 Correct 1199 ms 18284 KB Output is correct
52 Correct 1046 ms 18412 KB Output is correct
53 Correct 2072 ms 20716 KB Output is correct
54 Correct 1971 ms 20716 KB Output is correct
55 Correct 1638 ms 20844 KB Output is correct
56 Correct 619 ms 11632 KB Output is correct
57 Correct 625 ms 11624 KB Output is correct
58 Correct 2757 ms 23148 KB Output is correct
59 Correct 2692 ms 23148 KB Output is correct
60 Correct 2700 ms 23136 KB Output is correct
61 Correct 2802 ms 23276 KB Output is correct
62 Correct 1917 ms 21648 KB Output is correct
63 Correct 1932 ms 21628 KB Output is correct
64 Correct 2402 ms 22888 KB Output is correct
65 Correct 718 ms 10988 KB Output is correct
66 Correct 1933 ms 21224 KB Output is correct
67 Correct 2148 ms 20128 KB Output is correct
68 Correct 1925 ms 21228 KB Output is correct
69 Correct 1648 ms 21224 KB Output is correct
70 Correct 2099 ms 20644 KB Output is correct
71 Correct 2185 ms 20540 KB Output is correct
72 Correct 2167 ms 20584 KB Output is correct
73 Correct 2009 ms 21228 KB Output is correct
74 Correct 2029 ms 21228 KB Output is correct
75 Correct 2049 ms 21228 KB Output is correct
76 Correct 1705 ms 21228 KB Output is correct
77 Correct 1679 ms 21228 KB Output is correct
78 Correct 1751 ms 21228 KB Output is correct
79 Correct 1349 ms 19384 KB Output is correct
80 Correct 1312 ms 19308 KB Output is correct
81 Correct 1285 ms 19304 KB Output is correct
82 Correct 1577 ms 22508 KB Output is correct
83 Correct 1476 ms 22652 KB Output is correct
84 Correct 1547 ms 22536 KB Output is correct
85 Execution timed out 3093 ms 25536 KB Time limit exceeded
86 Halted 0 ms 0 KB -