Submission #254682

# Submission time Handle Problem Language Result Execution time Memory
254682 2020-07-30T12:39:58 Z Mahdi_Jfri Bridges (APIO19_bridges) C++14
73 / 100
3000 ms 24844 KB
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define lb(a) ((a)&(-(a)))

const int maxn = 1e5 + 20;
const int sq = 1000;

int from[maxn] , to[maxn] , w[maxn];

bool has[maxn];

int e[maxn] , nw[maxn] , s[maxn] , tmpw[maxn] , ans[maxn] , type[maxn];

vector<int> edges[maxn * 2] , query[maxn * 2];

vector<pair<int , int> > history;

int par[maxn / 2];

int root(int v , bool compress = 1)
{
	if(compress)
		return (par[v] < 0? v : par[v] = root(par[v]));

	while(par[v] >= 0)
		v = par[v];
	return v;
}

void merge(int a , int b , bool save = 0 , bool compress = 1)
{
	a = root(a , compress) , b = root(b , compress);

	if(a == b)
	{
		if(save)
			history.pb({-1 , -1});
		return;
	}

	if(-par[a] < -par[b])
		swap(a , b);
	
	if(save)
		history.pb({par[b] , b});
	par[a] += par[b];
	par[b] = a;
}

void undo()
{
	auto tmp = history.back();
	history.pop_back();

	if(tmp.second == -1)
		return;

	int b = tmp.second;
	int a = par[b];

	par[b] = tmp.first;
	par[a] -= par[b];
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	int n , m;
	cin >> n >> m;

	vector<int> cmp;
	for(int i = 0; i < m; i++)
	{
		int a , b;
		cin >> a >> b >> w[i];
		a-- , b--;

		from[i] = a , to[i] = b;
		cmp.pb(w[i]);
	}

	int q;
	cin >> q;

	for(int i = 0; i < q; i++)
	{
		cin >> type[i];
		if(type[i] == 1)
		{
			cin >> e[i] >> nw[i];
			e[i]--;
		}
		else
		{
			cin >> s[i] >> nw[i];
			s[i]--;
		}
		cmp.pb(nw[i]);
	}

	sort(cmp.begin() , cmp.end());
	cmp.resize(unique(cmp.begin() , cmp.end()) - cmp.begin());

	for(int i = 0; i < m; i++)
		w[i] = lower_bound(cmp.begin() , cmp.end() , w[i]) - cmp.begin();

	for(int i = 0; i < q; i++)
		nw[i] = lower_bound(cmp.begin() , cmp.end() , nw[i]) - cmp.begin();

	for(int l = 0; l < q; l += sq)
	{
		memset(has , 0 , sizeof has);
		memset(par , -1 , sizeof par);

		int sz = min(sq , q - l);
		vector<int> tmp;
		for(int i = l; i < l + sz; i++)
		{
			if(type[i] == 1)
				has[e[i]] = 1 , tmp.pb(e[i]);
			else
				query[nw[i]].pb(i);
		}

		for(int i = 0; i < m; i++)
			if(!has[i])
				edges[w[i]].pb(i);

		sort(tmp.begin() , tmp.end());
		tmp.resize(unique(tmp.begin() , tmp.end()) - tmp.begin());

		for(int i = (int)cmp.size() - 1; i >= 0; i--)
		{
			for(auto e : edges[i])
				merge(from[e] , to[e]);

			for(auto ind : query[i])
			{
				for(auto e : tmp)
					tmpw[e] = w[e];
				for(int j = l; j < ind; j++)
					if(type[j] == 1)
						tmpw[e[j]] = nw[j];

				int sz = 0;
				for(auto e : tmp)
					if(tmpw[e] >= i)
					{
						merge(from[e] , to[e] , 1 , 0);
						sz++;
					}

				ans[ind] = -par[root(s[ind] , 0)];

				while(sz--)
					undo();
			}

			edges[i].clear();
			query[i].clear();
		}

		for(int i = l; i < l + sz; i++)
			if(type[i] == 1)
				w[e[i]] = nw[i];
	}

	for(int i = 0; i < q; i++)
		if(type[i] == 2)
			cout << ans[i] << endl;
}





# Verdict Execution time Memory Grader output
1 Correct 7 ms 10112 KB Output is correct
2 Correct 6 ms 10112 KB Output is correct
3 Correct 59 ms 10744 KB Output is correct
4 Correct 35 ms 10624 KB Output is correct
5 Correct 34 ms 10496 KB Output is correct
6 Correct 32 ms 10368 KB Output is correct
7 Correct 33 ms 10368 KB Output is correct
8 Correct 35 ms 10496 KB Output is correct
9 Correct 36 ms 10368 KB Output is correct
10 Correct 35 ms 10496 KB Output is correct
11 Correct 36 ms 10496 KB Output is correct
12 Correct 37 ms 10616 KB Output is correct
13 Correct 42 ms 10488 KB Output is correct
14 Correct 41 ms 10492 KB Output is correct
15 Correct 38 ms 10496 KB Output is correct
16 Correct 33 ms 10428 KB Output is correct
17 Correct 35 ms 10368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1723 ms 18344 KB Output is correct
2 Correct 1719 ms 18252 KB Output is correct
3 Correct 1848 ms 18180 KB Output is correct
4 Correct 1776 ms 18248 KB Output is correct
5 Correct 1837 ms 18288 KB Output is correct
6 Correct 2266 ms 17968 KB Output is correct
7 Correct 2282 ms 18132 KB Output is correct
8 Correct 2408 ms 18400 KB Output is correct
9 Correct 341 ms 15332 KB Output is correct
10 Correct 1189 ms 14192 KB Output is correct
11 Correct 1112 ms 14188 KB Output is correct
12 Correct 1418 ms 18244 KB Output is correct
13 Correct 1436 ms 18192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1368 ms 17432 KB Output is correct
2 Correct 961 ms 15288 KB Output is correct
3 Correct 1498 ms 16876 KB Output is correct
4 Correct 1318 ms 17264 KB Output is correct
5 Correct 339 ms 15348 KB Output is correct
6 Correct 1445 ms 17368 KB Output is correct
7 Correct 1226 ms 17392 KB Output is correct
8 Correct 1292 ms 17348 KB Output is correct
9 Correct 1043 ms 17640 KB Output is correct
10 Correct 963 ms 17596 KB Output is correct
11 Correct 874 ms 17264 KB Output is correct
12 Correct 797 ms 17008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2280 ms 20664 KB Output is correct
2 Correct 368 ms 16760 KB Output is correct
3 Correct 222 ms 17140 KB Output is correct
4 Correct 96 ms 14452 KB Output is correct
5 Correct 745 ms 17004 KB Output is correct
6 Correct 2459 ms 24192 KB Output is correct
7 Correct 683 ms 17136 KB Output is correct
8 Correct 1111 ms 20588 KB Output is correct
9 Correct 1102 ms 20588 KB Output is correct
10 Correct 924 ms 20464 KB Output is correct
11 Correct 1866 ms 22328 KB Output is correct
12 Correct 1712 ms 22412 KB Output is correct
13 Correct 1410 ms 22260 KB Output is correct
14 Correct 682 ms 17388 KB Output is correct
15 Correct 698 ms 17132 KB Output is correct
16 Correct 2405 ms 24076 KB Output is correct
17 Correct 2519 ms 23976 KB Output is correct
18 Correct 2322 ms 24104 KB Output is correct
19 Correct 2321 ms 24296 KB Output is correct
20 Correct 1653 ms 22668 KB Output is correct
21 Correct 1747 ms 22640 KB Output is correct
22 Correct 1924 ms 24036 KB Output is correct
23 Correct 723 ms 16456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1723 ms 18344 KB Output is correct
2 Correct 1719 ms 18252 KB Output is correct
3 Correct 1848 ms 18180 KB Output is correct
4 Correct 1776 ms 18248 KB Output is correct
5 Correct 1837 ms 18288 KB Output is correct
6 Correct 2266 ms 17968 KB Output is correct
7 Correct 2282 ms 18132 KB Output is correct
8 Correct 2408 ms 18400 KB Output is correct
9 Correct 341 ms 15332 KB Output is correct
10 Correct 1189 ms 14192 KB Output is correct
11 Correct 1112 ms 14188 KB Output is correct
12 Correct 1418 ms 18244 KB Output is correct
13 Correct 1436 ms 18192 KB Output is correct
14 Correct 1368 ms 17432 KB Output is correct
15 Correct 961 ms 15288 KB Output is correct
16 Correct 1498 ms 16876 KB Output is correct
17 Correct 1318 ms 17264 KB Output is correct
18 Correct 339 ms 15348 KB Output is correct
19 Correct 1445 ms 17368 KB Output is correct
20 Correct 1226 ms 17392 KB Output is correct
21 Correct 1292 ms 17348 KB Output is correct
22 Correct 1043 ms 17640 KB Output is correct
23 Correct 963 ms 17596 KB Output is correct
24 Correct 874 ms 17264 KB Output is correct
25 Correct 797 ms 17008 KB Output is correct
26 Correct 1705 ms 18272 KB Output is correct
27 Correct 2057 ms 17540 KB Output is correct
28 Correct 2033 ms 18472 KB Output is correct
29 Correct 1405 ms 18160 KB Output is correct
30 Correct 2011 ms 17772 KB Output is correct
31 Correct 1975 ms 17776 KB Output is correct
32 Correct 2155 ms 17728 KB Output is correct
33 Correct 1794 ms 18112 KB Output is correct
34 Correct 1798 ms 18144 KB Output is correct
35 Correct 1827 ms 18160 KB Output is correct
36 Correct 1542 ms 18232 KB Output is correct
37 Correct 1589 ms 18160 KB Output is correct
38 Correct 1432 ms 18160 KB Output is correct
39 Correct 1208 ms 18284 KB Output is correct
40 Correct 1183 ms 18284 KB Output is correct
41 Correct 1189 ms 18544 KB Output is correct
42 Correct 1150 ms 18032 KB Output is correct
43 Correct 1354 ms 18096 KB Output is correct
44 Correct 1152 ms 18288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10112 KB Output is correct
2 Correct 6 ms 10112 KB Output is correct
3 Correct 59 ms 10744 KB Output is correct
4 Correct 35 ms 10624 KB Output is correct
5 Correct 34 ms 10496 KB Output is correct
6 Correct 32 ms 10368 KB Output is correct
7 Correct 33 ms 10368 KB Output is correct
8 Correct 35 ms 10496 KB Output is correct
9 Correct 36 ms 10368 KB Output is correct
10 Correct 35 ms 10496 KB Output is correct
11 Correct 36 ms 10496 KB Output is correct
12 Correct 37 ms 10616 KB Output is correct
13 Correct 42 ms 10488 KB Output is correct
14 Correct 41 ms 10492 KB Output is correct
15 Correct 38 ms 10496 KB Output is correct
16 Correct 33 ms 10428 KB Output is correct
17 Correct 35 ms 10368 KB Output is correct
18 Correct 1723 ms 18344 KB Output is correct
19 Correct 1719 ms 18252 KB Output is correct
20 Correct 1848 ms 18180 KB Output is correct
21 Correct 1776 ms 18248 KB Output is correct
22 Correct 1837 ms 18288 KB Output is correct
23 Correct 2266 ms 17968 KB Output is correct
24 Correct 2282 ms 18132 KB Output is correct
25 Correct 2408 ms 18400 KB Output is correct
26 Correct 341 ms 15332 KB Output is correct
27 Correct 1189 ms 14192 KB Output is correct
28 Correct 1112 ms 14188 KB Output is correct
29 Correct 1418 ms 18244 KB Output is correct
30 Correct 1436 ms 18192 KB Output is correct
31 Correct 1368 ms 17432 KB Output is correct
32 Correct 961 ms 15288 KB Output is correct
33 Correct 1498 ms 16876 KB Output is correct
34 Correct 1318 ms 17264 KB Output is correct
35 Correct 339 ms 15348 KB Output is correct
36 Correct 1445 ms 17368 KB Output is correct
37 Correct 1226 ms 17392 KB Output is correct
38 Correct 1292 ms 17348 KB Output is correct
39 Correct 1043 ms 17640 KB Output is correct
40 Correct 963 ms 17596 KB Output is correct
41 Correct 874 ms 17264 KB Output is correct
42 Correct 797 ms 17008 KB Output is correct
43 Correct 2280 ms 20664 KB Output is correct
44 Correct 368 ms 16760 KB Output is correct
45 Correct 222 ms 17140 KB Output is correct
46 Correct 96 ms 14452 KB Output is correct
47 Correct 745 ms 17004 KB Output is correct
48 Correct 2459 ms 24192 KB Output is correct
49 Correct 683 ms 17136 KB Output is correct
50 Correct 1111 ms 20588 KB Output is correct
51 Correct 1102 ms 20588 KB Output is correct
52 Correct 924 ms 20464 KB Output is correct
53 Correct 1866 ms 22328 KB Output is correct
54 Correct 1712 ms 22412 KB Output is correct
55 Correct 1410 ms 22260 KB Output is correct
56 Correct 682 ms 17388 KB Output is correct
57 Correct 698 ms 17132 KB Output is correct
58 Correct 2405 ms 24076 KB Output is correct
59 Correct 2519 ms 23976 KB Output is correct
60 Correct 2322 ms 24104 KB Output is correct
61 Correct 2321 ms 24296 KB Output is correct
62 Correct 1653 ms 22668 KB Output is correct
63 Correct 1747 ms 22640 KB Output is correct
64 Correct 1924 ms 24036 KB Output is correct
65 Correct 723 ms 16456 KB Output is correct
66 Correct 1705 ms 18272 KB Output is correct
67 Correct 2057 ms 17540 KB Output is correct
68 Correct 2033 ms 18472 KB Output is correct
69 Correct 1405 ms 18160 KB Output is correct
70 Correct 2011 ms 17772 KB Output is correct
71 Correct 1975 ms 17776 KB Output is correct
72 Correct 2155 ms 17728 KB Output is correct
73 Correct 1794 ms 18112 KB Output is correct
74 Correct 1798 ms 18144 KB Output is correct
75 Correct 1827 ms 18160 KB Output is correct
76 Correct 1542 ms 18232 KB Output is correct
77 Correct 1589 ms 18160 KB Output is correct
78 Correct 1432 ms 18160 KB Output is correct
79 Correct 1208 ms 18284 KB Output is correct
80 Correct 1183 ms 18284 KB Output is correct
81 Correct 1189 ms 18544 KB Output is correct
82 Correct 1150 ms 18032 KB Output is correct
83 Correct 1354 ms 18096 KB Output is correct
84 Correct 1152 ms 18288 KB Output is correct
85 Correct 2795 ms 24700 KB Output is correct
86 Correct 231 ms 17664 KB Output is correct
87 Correct 116 ms 14832 KB Output is correct
88 Correct 1066 ms 17776 KB Output is correct
89 Correct 2908 ms 24556 KB Output is correct
90 Correct 1094 ms 17388 KB Output is correct
91 Correct 1829 ms 20880 KB Output is correct
92 Correct 1845 ms 21100 KB Output is correct
93 Correct 2473 ms 20788 KB Output is correct
94 Correct 2402 ms 22676 KB Output is correct
95 Correct 2331 ms 22908 KB Output is correct
96 Correct 2486 ms 22640 KB Output is correct
97 Correct 854 ms 17668 KB Output is correct
98 Correct 711 ms 17204 KB Output is correct
99 Execution timed out 3022 ms 24844 KB Time limit exceeded
100 Halted 0 ms 0 KB -