답안 #209507

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

struct DSU {
	int n;
	int cnt;
	vector<int> aff;
	
	DSU(int _n) {
		n = _n;
		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<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;
	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>());
	
	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;
		E.reserve(y - x + 1);
		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:182: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 6392 KB Output is correct
3 Correct 64 ms 7288 KB Output is correct
4 Correct 18 ms 7032 KB Output is correct
5 Correct 33 ms 7032 KB Output is correct
6 Correct 30 ms 6776 KB Output is correct
7 Correct 32 ms 6648 KB Output is correct
8 Correct 31 ms 7032 KB Output is correct
9 Correct 41 ms 6648 KB Output is correct
10 Correct 31 ms 7032 KB Output is correct
11 Correct 31 ms 7064 KB Output is correct
12 Correct 33 ms 7032 KB Output is correct
13 Correct 40 ms 7036 KB Output is correct
14 Correct 38 ms 7032 KB Output is correct
15 Correct 37 ms 7032 KB Output is correct
16 Correct 34 ms 6648 KB Output is correct
17 Correct 33 ms 6648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2185 ms 21216 KB Output is correct
2 Correct 2053 ms 21224 KB Output is correct
3 Correct 2041 ms 21224 KB Output is correct
4 Correct 2063 ms 21324 KB Output is correct
5 Correct 2048 ms 21224 KB Output is correct
6 Correct 2549 ms 19436 KB Output is correct
7 Correct 2433 ms 19816 KB Output is correct
8 Correct 2444 ms 20132 KB Output is correct
9 Correct 160 ms 13040 KB Output is correct
10 Correct 1387 ms 11244 KB Output is correct
11 Correct 1239 ms 11372 KB Output is correct
12 Correct 1421 ms 18432 KB Output is correct
13 Correct 1756 ms 21608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1605 ms 19048 KB Output is correct
2 Correct 1118 ms 14704 KB Output is correct
3 Correct 1809 ms 18028 KB Output is correct
4 Correct 1582 ms 19048 KB Output is correct
5 Correct 159 ms 13168 KB Output is correct
6 Correct 1709 ms 19048 KB Output is correct
7 Correct 1463 ms 19056 KB Output is correct
8 Correct 1263 ms 19048 KB Output is correct
9 Correct 1014 ms 17512 KB Output is correct
10 Correct 932 ms 17388 KB Output is correct
11 Correct 1153 ms 20332 KB Output is correct
12 Correct 1042 ms 20364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2608 ms 23276 KB Output is correct
2 Correct 160 ms 13168 KB Output is correct
3 Correct 243 ms 16408 KB Output is correct
4 Correct 53 ms 8940 KB Output is correct
5 Correct 619 ms 11500 KB Output is correct
6 Correct 2569 ms 23148 KB Output is correct
7 Correct 607 ms 11368 KB Output is correct
8 Correct 1165 ms 18280 KB Output is correct
9 Correct 1205 ms 18284 KB Output is correct
10 Correct 1000 ms 18408 KB Output is correct
11 Correct 1962 ms 20716 KB Output is correct
12 Correct 1937 ms 20712 KB Output is correct
13 Correct 1646 ms 20908 KB Output is correct
14 Correct 592 ms 11628 KB Output is correct
15 Correct 600 ms 11628 KB Output is correct
16 Correct 2739 ms 23148 KB Output is correct
17 Correct 2600 ms 23148 KB Output is correct
18 Correct 2704 ms 23276 KB Output is correct
19 Correct 2728 ms 23148 KB Output is correct
20 Correct 1738 ms 21612 KB Output is correct
21 Correct 1718 ms 21484 KB Output is correct
22 Correct 2228 ms 22896 KB Output is correct
23 Correct 699 ms 10988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2185 ms 21216 KB Output is correct
2 Correct 2053 ms 21224 KB Output is correct
3 Correct 2041 ms 21224 KB Output is correct
4 Correct 2063 ms 21324 KB Output is correct
5 Correct 2048 ms 21224 KB Output is correct
6 Correct 2549 ms 19436 KB Output is correct
7 Correct 2433 ms 19816 KB Output is correct
8 Correct 2444 ms 20132 KB Output is correct
9 Correct 160 ms 13040 KB Output is correct
10 Correct 1387 ms 11244 KB Output is correct
11 Correct 1239 ms 11372 KB Output is correct
12 Correct 1421 ms 18432 KB Output is correct
13 Correct 1756 ms 21608 KB Output is correct
14 Correct 1605 ms 19048 KB Output is correct
15 Correct 1118 ms 14704 KB Output is correct
16 Correct 1809 ms 18028 KB Output is correct
17 Correct 1582 ms 19048 KB Output is correct
18 Correct 159 ms 13168 KB Output is correct
19 Correct 1709 ms 19048 KB Output is correct
20 Correct 1463 ms 19056 KB Output is correct
21 Correct 1263 ms 19048 KB Output is correct
22 Correct 1014 ms 17512 KB Output is correct
23 Correct 932 ms 17388 KB Output is correct
24 Correct 1153 ms 20332 KB Output is correct
25 Correct 1042 ms 20364 KB Output is correct
26 Correct 2201 ms 21204 KB Output is correct
27 Correct 2598 ms 20204 KB Output is correct
28 Correct 2299 ms 21228 KB Output is correct
29 Correct 1776 ms 21228 KB Output is correct
30 Correct 2669 ms 20716 KB Output is correct
31 Correct 2833 ms 20620 KB Output is correct
32 Correct 2720 ms 20584 KB Output is correct
33 Correct 2616 ms 21228 KB Output is correct
34 Correct 2397 ms 21328 KB Output is correct
35 Correct 2417 ms 21228 KB Output is correct
36 Correct 1946 ms 21224 KB Output is correct
37 Correct 1871 ms 21228 KB Output is correct
38 Correct 2029 ms 21160 KB Output is correct
39 Correct 1429 ms 19396 KB Output is correct
40 Correct 1421 ms 19376 KB Output is correct
41 Correct 1438 ms 19308 KB Output is correct
42 Correct 1562 ms 22504 KB Output is correct
43 Correct 1548 ms 22508 KB Output is correct
44 Correct 1524 ms 22508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6264 KB Output is correct
2 Correct 8 ms 6392 KB Output is correct
3 Correct 64 ms 7288 KB Output is correct
4 Correct 18 ms 7032 KB Output is correct
5 Correct 33 ms 7032 KB Output is correct
6 Correct 30 ms 6776 KB Output is correct
7 Correct 32 ms 6648 KB Output is correct
8 Correct 31 ms 7032 KB Output is correct
9 Correct 41 ms 6648 KB Output is correct
10 Correct 31 ms 7032 KB Output is correct
11 Correct 31 ms 7064 KB Output is correct
12 Correct 33 ms 7032 KB Output is correct
13 Correct 40 ms 7036 KB Output is correct
14 Correct 38 ms 7032 KB Output is correct
15 Correct 37 ms 7032 KB Output is correct
16 Correct 34 ms 6648 KB Output is correct
17 Correct 33 ms 6648 KB Output is correct
18 Correct 2185 ms 21216 KB Output is correct
19 Correct 2053 ms 21224 KB Output is correct
20 Correct 2041 ms 21224 KB Output is correct
21 Correct 2063 ms 21324 KB Output is correct
22 Correct 2048 ms 21224 KB Output is correct
23 Correct 2549 ms 19436 KB Output is correct
24 Correct 2433 ms 19816 KB Output is correct
25 Correct 2444 ms 20132 KB Output is correct
26 Correct 160 ms 13040 KB Output is correct
27 Correct 1387 ms 11244 KB Output is correct
28 Correct 1239 ms 11372 KB Output is correct
29 Correct 1421 ms 18432 KB Output is correct
30 Correct 1756 ms 21608 KB Output is correct
31 Correct 1605 ms 19048 KB Output is correct
32 Correct 1118 ms 14704 KB Output is correct
33 Correct 1809 ms 18028 KB Output is correct
34 Correct 1582 ms 19048 KB Output is correct
35 Correct 159 ms 13168 KB Output is correct
36 Correct 1709 ms 19048 KB Output is correct
37 Correct 1463 ms 19056 KB Output is correct
38 Correct 1263 ms 19048 KB Output is correct
39 Correct 1014 ms 17512 KB Output is correct
40 Correct 932 ms 17388 KB Output is correct
41 Correct 1153 ms 20332 KB Output is correct
42 Correct 1042 ms 20364 KB Output is correct
43 Correct 2608 ms 23276 KB Output is correct
44 Correct 160 ms 13168 KB Output is correct
45 Correct 243 ms 16408 KB Output is correct
46 Correct 53 ms 8940 KB Output is correct
47 Correct 619 ms 11500 KB Output is correct
48 Correct 2569 ms 23148 KB Output is correct
49 Correct 607 ms 11368 KB Output is correct
50 Correct 1165 ms 18280 KB Output is correct
51 Correct 1205 ms 18284 KB Output is correct
52 Correct 1000 ms 18408 KB Output is correct
53 Correct 1962 ms 20716 KB Output is correct
54 Correct 1937 ms 20712 KB Output is correct
55 Correct 1646 ms 20908 KB Output is correct
56 Correct 592 ms 11628 KB Output is correct
57 Correct 600 ms 11628 KB Output is correct
58 Correct 2739 ms 23148 KB Output is correct
59 Correct 2600 ms 23148 KB Output is correct
60 Correct 2704 ms 23276 KB Output is correct
61 Correct 2728 ms 23148 KB Output is correct
62 Correct 1738 ms 21612 KB Output is correct
63 Correct 1718 ms 21484 KB Output is correct
64 Correct 2228 ms 22896 KB Output is correct
65 Correct 699 ms 10988 KB Output is correct
66 Correct 2201 ms 21204 KB Output is correct
67 Correct 2598 ms 20204 KB Output is correct
68 Correct 2299 ms 21228 KB Output is correct
69 Correct 1776 ms 21228 KB Output is correct
70 Correct 2669 ms 20716 KB Output is correct
71 Correct 2833 ms 20620 KB Output is correct
72 Correct 2720 ms 20584 KB Output is correct
73 Correct 2616 ms 21228 KB Output is correct
74 Correct 2397 ms 21328 KB Output is correct
75 Correct 2417 ms 21228 KB Output is correct
76 Correct 1946 ms 21224 KB Output is correct
77 Correct 1871 ms 21228 KB Output is correct
78 Correct 2029 ms 21160 KB Output is correct
79 Correct 1429 ms 19396 KB Output is correct
80 Correct 1421 ms 19376 KB Output is correct
81 Correct 1438 ms 19308 KB Output is correct
82 Correct 1562 ms 22504 KB Output is correct
83 Correct 1548 ms 22508 KB Output is correct
84 Correct 1524 ms 22508 KB Output is correct
85 Execution timed out 3100 ms 25452 KB Time limit exceeded
86 Halted 0 ms 0 KB -