답안 #209504

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
209504 2020-03-14T12:13:30 Z mieszko11b 다리 (APIO19_bridges) C++14
73 / 100
3000 ms 25444 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');
}

struct DSU {
	int n;
	vector<int> p, rank;
	int cnt;
	vector<bool> vis;
	vector<int> aff;
	
	DSU(int _n) {
		n = _n;
		p.resize(n + 1);
		rank.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);
	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:127: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:181:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < hlp.size() ; i++)
                  ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6264 KB Output is correct
2 Correct 8 ms 6268 KB Output is correct
3 Correct 73 ms 7288 KB Output is correct
4 Correct 17 ms 7032 KB Output is correct
5 Correct 37 ms 6904 KB Output is correct
6 Correct 34 ms 6776 KB Output is correct
7 Correct 35 ms 6520 KB Output is correct
8 Correct 34 ms 6904 KB Output is correct
9 Correct 46 ms 6520 KB Output is correct
10 Correct 37 ms 7032 KB Output is correct
11 Correct 34 ms 7032 KB Output is correct
12 Correct 37 ms 7032 KB Output is correct
13 Correct 46 ms 7032 KB Output is correct
14 Correct 43 ms 7032 KB Output is correct
15 Correct 39 ms 7032 KB Output is correct
16 Correct 38 ms 6520 KB Output is correct
17 Correct 36 ms 6520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2298 ms 21392 KB Output is correct
2 Correct 2126 ms 21352 KB Output is correct
3 Correct 2187 ms 21228 KB Output is correct
4 Correct 2158 ms 21480 KB Output is correct
5 Correct 2133 ms 21268 KB Output is correct
6 Correct 2763 ms 19564 KB Output is correct
7 Correct 2623 ms 19948 KB Output is correct
8 Correct 2584 ms 20200 KB Output is correct
9 Correct 155 ms 13168 KB Output is correct
10 Correct 1479 ms 11372 KB Output is correct
11 Correct 1346 ms 11372 KB Output is correct
12 Correct 1547 ms 18284 KB Output is correct
13 Correct 1884 ms 21848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1784 ms 19288 KB Output is correct
2 Correct 1238 ms 14608 KB Output is correct
3 Correct 1969 ms 18156 KB Output is correct
4 Correct 1738 ms 19176 KB Output is correct
5 Correct 159 ms 13168 KB Output is correct
6 Correct 1846 ms 19176 KB Output is correct
7 Correct 1640 ms 19068 KB Output is correct
8 Correct 1412 ms 19184 KB Output is correct
9 Correct 1121 ms 17392 KB Output is correct
10 Correct 1027 ms 17388 KB Output is correct
11 Correct 1235 ms 20460 KB Output is correct
12 Correct 1112 ms 20336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2744 ms 23208 KB Output is correct
2 Correct 160 ms 13168 KB Output is correct
3 Correct 269 ms 16368 KB Output is correct
4 Correct 52 ms 8944 KB Output is correct
5 Correct 658 ms 11496 KB Output is correct
6 Correct 2766 ms 23168 KB Output is correct
7 Correct 655 ms 11404 KB Output is correct
8 Correct 1188 ms 18288 KB Output is correct
9 Correct 1211 ms 18280 KB Output is correct
10 Correct 1038 ms 18412 KB Output is correct
11 Correct 1937 ms 20712 KB Output is correct
12 Correct 1941 ms 20716 KB Output is correct
13 Correct 1646 ms 20840 KB Output is correct
14 Correct 617 ms 11372 KB Output is correct
15 Correct 615 ms 11500 KB Output is correct
16 Correct 2622 ms 23148 KB Output is correct
17 Correct 2713 ms 23020 KB Output is correct
18 Correct 2679 ms 23148 KB Output is correct
19 Correct 2706 ms 23112 KB Output is correct
20 Correct 1840 ms 21612 KB Output is correct
21 Correct 1759 ms 21528 KB Output is correct
22 Correct 2271 ms 22892 KB Output is correct
23 Correct 686 ms 10988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2298 ms 21392 KB Output is correct
2 Correct 2126 ms 21352 KB Output is correct
3 Correct 2187 ms 21228 KB Output is correct
4 Correct 2158 ms 21480 KB Output is correct
5 Correct 2133 ms 21268 KB Output is correct
6 Correct 2763 ms 19564 KB Output is correct
7 Correct 2623 ms 19948 KB Output is correct
8 Correct 2584 ms 20200 KB Output is correct
9 Correct 155 ms 13168 KB Output is correct
10 Correct 1479 ms 11372 KB Output is correct
11 Correct 1346 ms 11372 KB Output is correct
12 Correct 1547 ms 18284 KB Output is correct
13 Correct 1884 ms 21848 KB Output is correct
14 Correct 1784 ms 19288 KB Output is correct
15 Correct 1238 ms 14608 KB Output is correct
16 Correct 1969 ms 18156 KB Output is correct
17 Correct 1738 ms 19176 KB Output is correct
18 Correct 159 ms 13168 KB Output is correct
19 Correct 1846 ms 19176 KB Output is correct
20 Correct 1640 ms 19068 KB Output is correct
21 Correct 1412 ms 19184 KB Output is correct
22 Correct 1121 ms 17392 KB Output is correct
23 Correct 1027 ms 17388 KB Output is correct
24 Correct 1235 ms 20460 KB Output is correct
25 Correct 1112 ms 20336 KB Output is correct
26 Correct 2338 ms 21352 KB Output is correct
27 Correct 2742 ms 20156 KB Output is correct
28 Correct 2488 ms 21228 KB Output is correct
29 Correct 1823 ms 21256 KB Output is correct
30 Correct 2851 ms 20840 KB Output is correct
31 Correct 2792 ms 20588 KB Output is correct
32 Correct 2798 ms 20768 KB Output is correct
33 Correct 2496 ms 21440 KB Output is correct
34 Correct 2486 ms 21208 KB Output is correct
35 Correct 2491 ms 21224 KB Output is correct
36 Correct 2005 ms 21228 KB Output is correct
37 Correct 1937 ms 21192 KB Output is correct
38 Correct 1936 ms 21208 KB Output is correct
39 Correct 1450 ms 19436 KB Output is correct
40 Correct 1424 ms 19436 KB Output is correct
41 Correct 1436 ms 19432 KB Output is correct
42 Correct 1624 ms 22520 KB Output is correct
43 Correct 1643 ms 22564 KB Output is correct
44 Correct 1646 ms 22508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6264 KB Output is correct
2 Correct 8 ms 6268 KB Output is correct
3 Correct 73 ms 7288 KB Output is correct
4 Correct 17 ms 7032 KB Output is correct
5 Correct 37 ms 6904 KB Output is correct
6 Correct 34 ms 6776 KB Output is correct
7 Correct 35 ms 6520 KB Output is correct
8 Correct 34 ms 6904 KB Output is correct
9 Correct 46 ms 6520 KB Output is correct
10 Correct 37 ms 7032 KB Output is correct
11 Correct 34 ms 7032 KB Output is correct
12 Correct 37 ms 7032 KB Output is correct
13 Correct 46 ms 7032 KB Output is correct
14 Correct 43 ms 7032 KB Output is correct
15 Correct 39 ms 7032 KB Output is correct
16 Correct 38 ms 6520 KB Output is correct
17 Correct 36 ms 6520 KB Output is correct
18 Correct 2298 ms 21392 KB Output is correct
19 Correct 2126 ms 21352 KB Output is correct
20 Correct 2187 ms 21228 KB Output is correct
21 Correct 2158 ms 21480 KB Output is correct
22 Correct 2133 ms 21268 KB Output is correct
23 Correct 2763 ms 19564 KB Output is correct
24 Correct 2623 ms 19948 KB Output is correct
25 Correct 2584 ms 20200 KB Output is correct
26 Correct 155 ms 13168 KB Output is correct
27 Correct 1479 ms 11372 KB Output is correct
28 Correct 1346 ms 11372 KB Output is correct
29 Correct 1547 ms 18284 KB Output is correct
30 Correct 1884 ms 21848 KB Output is correct
31 Correct 1784 ms 19288 KB Output is correct
32 Correct 1238 ms 14608 KB Output is correct
33 Correct 1969 ms 18156 KB Output is correct
34 Correct 1738 ms 19176 KB Output is correct
35 Correct 159 ms 13168 KB Output is correct
36 Correct 1846 ms 19176 KB Output is correct
37 Correct 1640 ms 19068 KB Output is correct
38 Correct 1412 ms 19184 KB Output is correct
39 Correct 1121 ms 17392 KB Output is correct
40 Correct 1027 ms 17388 KB Output is correct
41 Correct 1235 ms 20460 KB Output is correct
42 Correct 1112 ms 20336 KB Output is correct
43 Correct 2744 ms 23208 KB Output is correct
44 Correct 160 ms 13168 KB Output is correct
45 Correct 269 ms 16368 KB Output is correct
46 Correct 52 ms 8944 KB Output is correct
47 Correct 658 ms 11496 KB Output is correct
48 Correct 2766 ms 23168 KB Output is correct
49 Correct 655 ms 11404 KB Output is correct
50 Correct 1188 ms 18288 KB Output is correct
51 Correct 1211 ms 18280 KB Output is correct
52 Correct 1038 ms 18412 KB Output is correct
53 Correct 1937 ms 20712 KB Output is correct
54 Correct 1941 ms 20716 KB Output is correct
55 Correct 1646 ms 20840 KB Output is correct
56 Correct 617 ms 11372 KB Output is correct
57 Correct 615 ms 11500 KB Output is correct
58 Correct 2622 ms 23148 KB Output is correct
59 Correct 2713 ms 23020 KB Output is correct
60 Correct 2679 ms 23148 KB Output is correct
61 Correct 2706 ms 23112 KB Output is correct
62 Correct 1840 ms 21612 KB Output is correct
63 Correct 1759 ms 21528 KB Output is correct
64 Correct 2271 ms 22892 KB Output is correct
65 Correct 686 ms 10988 KB Output is correct
66 Correct 2338 ms 21352 KB Output is correct
67 Correct 2742 ms 20156 KB Output is correct
68 Correct 2488 ms 21228 KB Output is correct
69 Correct 1823 ms 21256 KB Output is correct
70 Correct 2851 ms 20840 KB Output is correct
71 Correct 2792 ms 20588 KB Output is correct
72 Correct 2798 ms 20768 KB Output is correct
73 Correct 2496 ms 21440 KB Output is correct
74 Correct 2486 ms 21208 KB Output is correct
75 Correct 2491 ms 21224 KB Output is correct
76 Correct 2005 ms 21228 KB Output is correct
77 Correct 1937 ms 21192 KB Output is correct
78 Correct 1936 ms 21208 KB Output is correct
79 Correct 1450 ms 19436 KB Output is correct
80 Correct 1424 ms 19436 KB Output is correct
81 Correct 1436 ms 19432 KB Output is correct
82 Correct 1624 ms 22520 KB Output is correct
83 Correct 1643 ms 22564 KB Output is correct
84 Correct 1646 ms 22508 KB Output is correct
85 Execution timed out 3098 ms 25444 KB Time limit exceeded
86 Halted 0 ms 0 KB -