Submission #209501

# Submission time Handle Problem Language Result Execution time Memory
209501 2020-03-14T12:02:46 Z mieszko11b Bridges (APIO19_bridges) C++14
73 / 100
3000 ms 27992 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];

struct DSU {
	int n;
	vector<int> p, rank;
	vector<vector<int>> G;
	int cnt;
	vector<bool> vis;
	vector<int> aff;
	
	DSU(int _n) {
		n = _n;
		p.resize(n + 1);
		rank.resize(n + 1);
		G.resize(n + 1);
		vis.resize(n + 1);
		for(int i = 1 ; i <= n ; i++) {
			p[i] = i;
			rank[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(rank[a] < rank[b]) {
			p[a] = b;
			rank[b] += rank[a];
		} else {
			p[b] = a;
			rank[a] += rank[b];
		}
	}
	
	void dfs(int w) {
		if(vis[w])
			return ;
		vis[w] = 1;
		aff.push_back(w);
		cnt += rank[w];
		for(int u : G[w])
			dfs(u);
	}
	
	int count(vector<int> &E, int w) {
		for(int e : E) {
			G[Find(a[e])].push_back(Find(b[e]));
			G[Find(b[e])].push_back(Find(a[e]));
		}
		cnt = 0;
		dfs(Find(w));
		for(int x : aff)
			vis[x] = 0;
		aff.clear();
		for(int e : E) {
			G[Find(a[e])].clear();
			G[Find(b[e])].clear();
		}
		return cnt;
	}
};


void process_queries(int x, int y) {
	vector<ii> Q;
	vector<int> chacha;
	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>());
	
	DSU *dsu = new DSU(n);
	
	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])
				dsu->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<int> E;
		for(int e : chacha)
			if(lastw[e] >= ::e[q])
				E.push_back(e);
		ans[q] = dsu->count(E, d[q]);
	}

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

int main() {
	scanf("%d%d",&n, &m);
	
	vector<int> hlp;
	
	for(int i = 1 ; i <= m ; i++) {
		scanf("%d%d%d", &a[i], &b[i], &c[i]);
		hlp.push_back(c[i]);
	}
	scanf("%d", &q);
	for(int i = 1 ; i <= q ; i++) {
		scanf("%d%d%d", &t[i], &d[i], &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:115: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:160:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < hlp.size() ; i++)
                  ~~^~~~~~~~~~~~
bridges.cpp:143:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d",&n, &m);
  ~~~~~^~~~~~~~~~~~~~~
bridges.cpp:148:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &a[i], &b[i], &c[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:151:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &q);
  ~~~~~^~~~~~~~~~
bridges.cpp:153:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &t[i], &d[i], &e[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5116 KB Output is correct
2 Correct 7 ms 5112 KB Output is correct
3 Correct 72 ms 6008 KB Output is correct
4 Correct 19 ms 5880 KB Output is correct
5 Correct 38 ms 5752 KB Output is correct
6 Correct 35 ms 5496 KB Output is correct
7 Correct 37 ms 5368 KB Output is correct
8 Correct 36 ms 5752 KB Output is correct
9 Correct 46 ms 5368 KB Output is correct
10 Correct 39 ms 5880 KB Output is correct
11 Correct 36 ms 5880 KB Output is correct
12 Correct 40 ms 5880 KB Output is correct
13 Correct 47 ms 5880 KB Output is correct
14 Correct 44 ms 5880 KB Output is correct
15 Correct 42 ms 5880 KB Output is correct
16 Correct 39 ms 5368 KB Output is correct
17 Correct 39 ms 5368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2372 ms 20044 KB Output is correct
2 Correct 2266 ms 19928 KB Output is correct
3 Correct 2266 ms 19928 KB Output is correct
4 Correct 2265 ms 19928 KB Output is correct
5 Correct 2239 ms 19920 KB Output is correct
6 Correct 2775 ms 19160 KB Output is correct
7 Correct 2757 ms 22104 KB Output is correct
8 Correct 2704 ms 22360 KB Output is correct
9 Correct 188 ms 13292 KB Output is correct
10 Correct 1513 ms 11860 KB Output is correct
11 Correct 1375 ms 11856 KB Output is correct
12 Correct 1664 ms 20696 KB Output is correct
13 Correct 1946 ms 23904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1750 ms 17756 KB Output is correct
2 Correct 1208 ms 13424 KB Output is correct
3 Correct 1921 ms 16648 KB Output is correct
4 Correct 1721 ms 17760 KB Output is correct
5 Correct 188 ms 12012 KB Output is correct
6 Correct 1841 ms 17816 KB Output is correct
7 Correct 1536 ms 17760 KB Output is correct
8 Correct 1414 ms 17772 KB Output is correct
9 Correct 1128 ms 16608 KB Output is correct
10 Correct 1069 ms 16608 KB Output is correct
11 Correct 1262 ms 18784 KB Output is correct
12 Correct 1123 ms 18784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2838 ms 23120 KB Output is correct
2 Correct 184 ms 12016 KB Output is correct
3 Correct 303 ms 15216 KB Output is correct
4 Correct 81 ms 7660 KB Output is correct
5 Correct 707 ms 11480 KB Output is correct
6 Correct 2817 ms 23072 KB Output is correct
7 Correct 720 ms 11344 KB Output is correct
8 Correct 1364 ms 18264 KB Output is correct
9 Correct 1328 ms 18264 KB Output is correct
10 Correct 1178 ms 18392 KB Output is correct
11 Correct 2114 ms 20688 KB Output is correct
12 Correct 2211 ms 20692 KB Output is correct
13 Correct 1777 ms 20816 KB Output is correct
14 Correct 694 ms 11340 KB Output is correct
15 Correct 697 ms 11472 KB Output is correct
16 Correct 2861 ms 23132 KB Output is correct
17 Correct 2849 ms 23124 KB Output is correct
18 Correct 2944 ms 23128 KB Output is correct
19 Correct 2898 ms 23128 KB Output is correct
20 Correct 1971 ms 21552 KB Output is correct
21 Correct 2020 ms 21444 KB Output is correct
22 Correct 2483 ms 22868 KB Output is correct
23 Correct 755 ms 10832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2372 ms 20044 KB Output is correct
2 Correct 2266 ms 19928 KB Output is correct
3 Correct 2266 ms 19928 KB Output is correct
4 Correct 2265 ms 19928 KB Output is correct
5 Correct 2239 ms 19920 KB Output is correct
6 Correct 2775 ms 19160 KB Output is correct
7 Correct 2757 ms 22104 KB Output is correct
8 Correct 2704 ms 22360 KB Output is correct
9 Correct 188 ms 13292 KB Output is correct
10 Correct 1513 ms 11860 KB Output is correct
11 Correct 1375 ms 11856 KB Output is correct
12 Correct 1664 ms 20696 KB Output is correct
13 Correct 1946 ms 23904 KB Output is correct
14 Correct 1750 ms 17756 KB Output is correct
15 Correct 1208 ms 13424 KB Output is correct
16 Correct 1921 ms 16648 KB Output is correct
17 Correct 1721 ms 17760 KB Output is correct
18 Correct 188 ms 12012 KB Output is correct
19 Correct 1841 ms 17816 KB Output is correct
20 Correct 1536 ms 17760 KB Output is correct
21 Correct 1414 ms 17772 KB Output is correct
22 Correct 1128 ms 16608 KB Output is correct
23 Correct 1069 ms 16608 KB Output is correct
24 Correct 1262 ms 18784 KB Output is correct
25 Correct 1123 ms 18784 KB Output is correct
26 Correct 2401 ms 22736 KB Output is correct
27 Correct 2855 ms 21460 KB Output is correct
28 Correct 2541 ms 22864 KB Output is correct
29 Correct 1960 ms 22780 KB Output is correct
30 Correct 2863 ms 22048 KB Output is correct
31 Correct 2858 ms 21884 KB Output is correct
32 Correct 2960 ms 21844 KB Output is correct
33 Correct 2565 ms 22872 KB Output is correct
34 Correct 2562 ms 22740 KB Output is correct
35 Correct 2585 ms 22744 KB Output is correct
36 Correct 2089 ms 22608 KB Output is correct
37 Correct 2089 ms 22612 KB Output is correct
38 Correct 2063 ms 22616 KB Output is correct
39 Correct 1581 ms 21456 KB Output is correct
40 Correct 1579 ms 21464 KB Output is correct
41 Correct 1554 ms 21456 KB Output is correct
42 Correct 1763 ms 23760 KB Output is correct
43 Correct 1768 ms 23740 KB Output is correct
44 Correct 1718 ms 23768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5116 KB Output is correct
2 Correct 7 ms 5112 KB Output is correct
3 Correct 72 ms 6008 KB Output is correct
4 Correct 19 ms 5880 KB Output is correct
5 Correct 38 ms 5752 KB Output is correct
6 Correct 35 ms 5496 KB Output is correct
7 Correct 37 ms 5368 KB Output is correct
8 Correct 36 ms 5752 KB Output is correct
9 Correct 46 ms 5368 KB Output is correct
10 Correct 39 ms 5880 KB Output is correct
11 Correct 36 ms 5880 KB Output is correct
12 Correct 40 ms 5880 KB Output is correct
13 Correct 47 ms 5880 KB Output is correct
14 Correct 44 ms 5880 KB Output is correct
15 Correct 42 ms 5880 KB Output is correct
16 Correct 39 ms 5368 KB Output is correct
17 Correct 39 ms 5368 KB Output is correct
18 Correct 2372 ms 20044 KB Output is correct
19 Correct 2266 ms 19928 KB Output is correct
20 Correct 2266 ms 19928 KB Output is correct
21 Correct 2265 ms 19928 KB Output is correct
22 Correct 2239 ms 19920 KB Output is correct
23 Correct 2775 ms 19160 KB Output is correct
24 Correct 2757 ms 22104 KB Output is correct
25 Correct 2704 ms 22360 KB Output is correct
26 Correct 188 ms 13292 KB Output is correct
27 Correct 1513 ms 11860 KB Output is correct
28 Correct 1375 ms 11856 KB Output is correct
29 Correct 1664 ms 20696 KB Output is correct
30 Correct 1946 ms 23904 KB Output is correct
31 Correct 1750 ms 17756 KB Output is correct
32 Correct 1208 ms 13424 KB Output is correct
33 Correct 1921 ms 16648 KB Output is correct
34 Correct 1721 ms 17760 KB Output is correct
35 Correct 188 ms 12012 KB Output is correct
36 Correct 1841 ms 17816 KB Output is correct
37 Correct 1536 ms 17760 KB Output is correct
38 Correct 1414 ms 17772 KB Output is correct
39 Correct 1128 ms 16608 KB Output is correct
40 Correct 1069 ms 16608 KB Output is correct
41 Correct 1262 ms 18784 KB Output is correct
42 Correct 1123 ms 18784 KB Output is correct
43 Correct 2838 ms 23120 KB Output is correct
44 Correct 184 ms 12016 KB Output is correct
45 Correct 303 ms 15216 KB Output is correct
46 Correct 81 ms 7660 KB Output is correct
47 Correct 707 ms 11480 KB Output is correct
48 Correct 2817 ms 23072 KB Output is correct
49 Correct 720 ms 11344 KB Output is correct
50 Correct 1364 ms 18264 KB Output is correct
51 Correct 1328 ms 18264 KB Output is correct
52 Correct 1178 ms 18392 KB Output is correct
53 Correct 2114 ms 20688 KB Output is correct
54 Correct 2211 ms 20692 KB Output is correct
55 Correct 1777 ms 20816 KB Output is correct
56 Correct 694 ms 11340 KB Output is correct
57 Correct 697 ms 11472 KB Output is correct
58 Correct 2861 ms 23132 KB Output is correct
59 Correct 2849 ms 23124 KB Output is correct
60 Correct 2944 ms 23128 KB Output is correct
61 Correct 2898 ms 23128 KB Output is correct
62 Correct 1971 ms 21552 KB Output is correct
63 Correct 2020 ms 21444 KB Output is correct
64 Correct 2483 ms 22868 KB Output is correct
65 Correct 755 ms 10832 KB Output is correct
66 Correct 2401 ms 22736 KB Output is correct
67 Correct 2855 ms 21460 KB Output is correct
68 Correct 2541 ms 22864 KB Output is correct
69 Correct 1960 ms 22780 KB Output is correct
70 Correct 2863 ms 22048 KB Output is correct
71 Correct 2858 ms 21884 KB Output is correct
72 Correct 2960 ms 21844 KB Output is correct
73 Correct 2565 ms 22872 KB Output is correct
74 Correct 2562 ms 22740 KB Output is correct
75 Correct 2585 ms 22744 KB Output is correct
76 Correct 2089 ms 22608 KB Output is correct
77 Correct 2089 ms 22612 KB Output is correct
78 Correct 2063 ms 22616 KB Output is correct
79 Correct 1581 ms 21456 KB Output is correct
80 Correct 1579 ms 21464 KB Output is correct
81 Correct 1554 ms 21456 KB Output is correct
82 Correct 1763 ms 23760 KB Output is correct
83 Correct 1768 ms 23740 KB Output is correct
84 Correct 1718 ms 23768 KB Output is correct
85 Execution timed out 3067 ms 27992 KB Time limit exceeded
86 Halted 0 ms 0 KB -