답안 #253746

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
253746 2020-07-28T15:35:09 Z Saboon 다리 (APIO19_bridges) C++14
73 / 100
3000 ms 7864 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 20;
const int SQ = 400;

int E1[maxn], E2[maxn], W[maxn], Wtmp[maxn], Q1[maxn], Q2[maxn], Qtype[maxn];
bool modify[maxn], visited[maxn];
vector<int> g[maxn];
int par[maxn], ans[maxn];

void dfsoff(int v){
	visited[v] = 0;
	for (auto u : g[v])
		if (visited[u])
			dfsoff(u);
}

int dfs(int v){
	int ret = -par[v];
	visited[v] = 1;
	for (auto u : g[v])
		if (!visited[u])
			ret += dfs(u);
	return ret;
}

int get_par(int v){ return par[v] < 0 ? v : par[v] = get_par(par[v]); }
void merge(int v, int u){
	if ((v = get_par(v)) == (u = get_par(u)))
		return;
	if (par[v] > par[u])
		swap(v, u);
	par[v] += par[u];
	par[u] = v;
}

int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	vector<int> Edge;
	for (int i = 1; i <= m; i++){
		scanf("%d%d%d", &E1[i], &E2[i], &W[i]);
		Edge.push_back(i);
	}
	int q;
	scanf("%d", &q);
	for (int i = 0; i < q; i++)
		scanf("%d%d%d", &Qtype[i], &Q1[i], &Q2[i]);
	for (int i = 0; i < q; i += SQ){
		memset(par, -1, sizeof par);
		int l = i, r = min(i+SQ,q);
		vector<int> a, b;
		for (int j = l; j < r; j++){
			if (Qtype[j] == 1)
				modify[Q1[j]] = 1, b.push_back(Q1[j]);
			if (Qtype[j] == 2)
				a.push_back(j);
		}
		sort(b.begin(), b.end());
		b.resize(unique(b.begin(),b.end())-b.begin());
		sort(a.begin(), a.end(), [](int x, int y){return Q2[x] > Q2[y];});
		sort(Edge.begin(), Edge.end(), [](int x, int y){return W[x] > W[y];});
		int ptr = 0;
		for (int x = l; x < r; x++)
			if (Qtype[x] == 1)
				Wtmp[Q1[x]] = W[Q1[x]];
		for (auto j : a){
			while (ptr < m and W[Edge[ptr]] >= Q2[j]){
				if (!modify[Edge[ptr]])
					merge(E1[Edge[ptr]], E2[Edge[ptr]]);
				ptr ++;
			}
			for (int x = l; x < j; x++)
				if (Qtype[x] == 1)
					W[Q1[x]] = Q2[x];
			for (auto x : b){
				if (W[x] >= Q2[j]){ 
					g[get_par(E1[x])].push_back(get_par(E2[x]));
					g[get_par(E2[x])].push_back(get_par(E1[x]));
				}
			}
			ans[j] = dfs(get_par(Q1[j]));
			dfsoff(get_par(Q1[j]));
			for (auto x : b)
				if (W[x] >= Q2[j])
					g[get_par(E1[x])].pop_back(), g[get_par(E2[x])].pop_back();
			for (int x = l; x < j; x++)
				if (Qtype[x] == 1)
					W[Q1[x]] = Wtmp[Q1[x]];
		}
		for (int j = l; j < r; j++)
			if (Qtype[j] == 1)
				modify[Q1[j]] = 0, W[Q1[j]] = Q2[j];
	}
	for (int i = 0; i < q; i++)
		if (Qtype[i] == 2)
			printf("%d\n", ans[i]);
}

Compilation message

bridges.cpp: In function 'int main()':
bridges.cpp:40: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:43:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &E1[i], &E2[i], &W[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &q);
  ~~~~~^~~~~~~~~~
bridges.cpp:49:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &Qtype[i], &Q1[i], &Q2[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3072 KB Output is correct
2 Correct 2 ms 3072 KB Output is correct
3 Correct 39 ms 3456 KB Output is correct
4 Correct 12 ms 3456 KB Output is correct
5 Correct 26 ms 3456 KB Output is correct
6 Correct 23 ms 3448 KB Output is correct
7 Correct 25 ms 3328 KB Output is correct
8 Correct 25 ms 3452 KB Output is correct
9 Correct 31 ms 3328 KB Output is correct
10 Correct 29 ms 3448 KB Output is correct
11 Correct 27 ms 3448 KB Output is correct
12 Correct 28 ms 3452 KB Output is correct
13 Correct 30 ms 3440 KB Output is correct
14 Correct 33 ms 3448 KB Output is correct
15 Correct 28 ms 3456 KB Output is correct
16 Correct 28 ms 3328 KB Output is correct
17 Correct 26 ms 3356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1784 ms 7328 KB Output is correct
2 Correct 1755 ms 7416 KB Output is correct
3 Correct 1692 ms 7476 KB Output is correct
4 Correct 1664 ms 7404 KB Output is correct
5 Correct 1691 ms 7412 KB Output is correct
6 Correct 2145 ms 6560 KB Output is correct
7 Correct 2067 ms 6704 KB Output is correct
8 Correct 1992 ms 6592 KB Output is correct
9 Correct 97 ms 5112 KB Output is correct
10 Correct 1380 ms 7624 KB Output is correct
11 Correct 1308 ms 7540 KB Output is correct
12 Correct 1542 ms 6616 KB Output is correct
13 Correct 1468 ms 6644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1231 ms 6648 KB Output is correct
2 Correct 721 ms 5368 KB Output is correct
3 Correct 1351 ms 6520 KB Output is correct
4 Correct 1228 ms 6520 KB Output is correct
5 Correct 100 ms 4856 KB Output is correct
6 Correct 1298 ms 6520 KB Output is correct
7 Correct 1146 ms 6520 KB Output is correct
8 Correct 1052 ms 6608 KB Output is correct
9 Correct 979 ms 6216 KB Output is correct
10 Correct 952 ms 6264 KB Output is correct
11 Correct 910 ms 6520 KB Output is correct
12 Correct 871 ms 6552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1785 ms 7132 KB Output is correct
2 Correct 118 ms 4984 KB Output is correct
3 Correct 188 ms 5104 KB Output is correct
4 Correct 123 ms 4976 KB Output is correct
5 Correct 1214 ms 6916 KB Output is correct
6 Correct 1782 ms 6980 KB Output is correct
7 Correct 1233 ms 7152 KB Output is correct
8 Correct 884 ms 6004 KB Output is correct
9 Correct 873 ms 6004 KB Output is correct
10 Correct 888 ms 6136 KB Output is correct
11 Correct 1499 ms 6296 KB Output is correct
12 Correct 1462 ms 6512 KB Output is correct
13 Correct 1497 ms 6888 KB Output is correct
14 Correct 1129 ms 7176 KB Output is correct
15 Correct 1205 ms 7044 KB Output is correct
16 Correct 1907 ms 6896 KB Output is correct
17 Correct 1959 ms 6896 KB Output is correct
18 Correct 1868 ms 7000 KB Output is correct
19 Correct 1870 ms 6912 KB Output is correct
20 Correct 1724 ms 6912 KB Output is correct
21 Correct 1712 ms 7000 KB Output is correct
22 Correct 1822 ms 7024 KB Output is correct
23 Correct 1358 ms 6768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1784 ms 7328 KB Output is correct
2 Correct 1755 ms 7416 KB Output is correct
3 Correct 1692 ms 7476 KB Output is correct
4 Correct 1664 ms 7404 KB Output is correct
5 Correct 1691 ms 7412 KB Output is correct
6 Correct 2145 ms 6560 KB Output is correct
7 Correct 2067 ms 6704 KB Output is correct
8 Correct 1992 ms 6592 KB Output is correct
9 Correct 97 ms 5112 KB Output is correct
10 Correct 1380 ms 7624 KB Output is correct
11 Correct 1308 ms 7540 KB Output is correct
12 Correct 1542 ms 6616 KB Output is correct
13 Correct 1468 ms 6644 KB Output is correct
14 Correct 1231 ms 6648 KB Output is correct
15 Correct 721 ms 5368 KB Output is correct
16 Correct 1351 ms 6520 KB Output is correct
17 Correct 1228 ms 6520 KB Output is correct
18 Correct 100 ms 4856 KB Output is correct
19 Correct 1298 ms 6520 KB Output is correct
20 Correct 1146 ms 6520 KB Output is correct
21 Correct 1052 ms 6608 KB Output is correct
22 Correct 979 ms 6216 KB Output is correct
23 Correct 952 ms 6264 KB Output is correct
24 Correct 910 ms 6520 KB Output is correct
25 Correct 871 ms 6552 KB Output is correct
26 Correct 1761 ms 7148 KB Output is correct
27 Correct 2055 ms 7388 KB Output is correct
28 Correct 1861 ms 7232 KB Output is correct
29 Correct 1549 ms 7156 KB Output is correct
30 Correct 2060 ms 7416 KB Output is correct
31 Correct 2047 ms 7448 KB Output is correct
32 Correct 2024 ms 7284 KB Output is correct
33 Correct 1857 ms 7156 KB Output is correct
34 Correct 1835 ms 7156 KB Output is correct
35 Correct 1866 ms 7304 KB Output is correct
36 Correct 1579 ms 7132 KB Output is correct
37 Correct 1603 ms 7204 KB Output is correct
38 Correct 1539 ms 7156 KB Output is correct
39 Correct 1413 ms 6516 KB Output is correct
40 Correct 1441 ms 6516 KB Output is correct
41 Correct 1449 ms 6572 KB Output is correct
42 Correct 1311 ms 7284 KB Output is correct
43 Correct 1327 ms 7284 KB Output is correct
44 Correct 1283 ms 7280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3072 KB Output is correct
2 Correct 2 ms 3072 KB Output is correct
3 Correct 39 ms 3456 KB Output is correct
4 Correct 12 ms 3456 KB Output is correct
5 Correct 26 ms 3456 KB Output is correct
6 Correct 23 ms 3448 KB Output is correct
7 Correct 25 ms 3328 KB Output is correct
8 Correct 25 ms 3452 KB Output is correct
9 Correct 31 ms 3328 KB Output is correct
10 Correct 29 ms 3448 KB Output is correct
11 Correct 27 ms 3448 KB Output is correct
12 Correct 28 ms 3452 KB Output is correct
13 Correct 30 ms 3440 KB Output is correct
14 Correct 33 ms 3448 KB Output is correct
15 Correct 28 ms 3456 KB Output is correct
16 Correct 28 ms 3328 KB Output is correct
17 Correct 26 ms 3356 KB Output is correct
18 Correct 1784 ms 7328 KB Output is correct
19 Correct 1755 ms 7416 KB Output is correct
20 Correct 1692 ms 7476 KB Output is correct
21 Correct 1664 ms 7404 KB Output is correct
22 Correct 1691 ms 7412 KB Output is correct
23 Correct 2145 ms 6560 KB Output is correct
24 Correct 2067 ms 6704 KB Output is correct
25 Correct 1992 ms 6592 KB Output is correct
26 Correct 97 ms 5112 KB Output is correct
27 Correct 1380 ms 7624 KB Output is correct
28 Correct 1308 ms 7540 KB Output is correct
29 Correct 1542 ms 6616 KB Output is correct
30 Correct 1468 ms 6644 KB Output is correct
31 Correct 1231 ms 6648 KB Output is correct
32 Correct 721 ms 5368 KB Output is correct
33 Correct 1351 ms 6520 KB Output is correct
34 Correct 1228 ms 6520 KB Output is correct
35 Correct 100 ms 4856 KB Output is correct
36 Correct 1298 ms 6520 KB Output is correct
37 Correct 1146 ms 6520 KB Output is correct
38 Correct 1052 ms 6608 KB Output is correct
39 Correct 979 ms 6216 KB Output is correct
40 Correct 952 ms 6264 KB Output is correct
41 Correct 910 ms 6520 KB Output is correct
42 Correct 871 ms 6552 KB Output is correct
43 Correct 1785 ms 7132 KB Output is correct
44 Correct 118 ms 4984 KB Output is correct
45 Correct 188 ms 5104 KB Output is correct
46 Correct 123 ms 4976 KB Output is correct
47 Correct 1214 ms 6916 KB Output is correct
48 Correct 1782 ms 6980 KB Output is correct
49 Correct 1233 ms 7152 KB Output is correct
50 Correct 884 ms 6004 KB Output is correct
51 Correct 873 ms 6004 KB Output is correct
52 Correct 888 ms 6136 KB Output is correct
53 Correct 1499 ms 6296 KB Output is correct
54 Correct 1462 ms 6512 KB Output is correct
55 Correct 1497 ms 6888 KB Output is correct
56 Correct 1129 ms 7176 KB Output is correct
57 Correct 1205 ms 7044 KB Output is correct
58 Correct 1907 ms 6896 KB Output is correct
59 Correct 1959 ms 6896 KB Output is correct
60 Correct 1868 ms 7000 KB Output is correct
61 Correct 1870 ms 6912 KB Output is correct
62 Correct 1724 ms 6912 KB Output is correct
63 Correct 1712 ms 7000 KB Output is correct
64 Correct 1822 ms 7024 KB Output is correct
65 Correct 1358 ms 6768 KB Output is correct
66 Correct 1761 ms 7148 KB Output is correct
67 Correct 2055 ms 7388 KB Output is correct
68 Correct 1861 ms 7232 KB Output is correct
69 Correct 1549 ms 7156 KB Output is correct
70 Correct 2060 ms 7416 KB Output is correct
71 Correct 2047 ms 7448 KB Output is correct
72 Correct 2024 ms 7284 KB Output is correct
73 Correct 1857 ms 7156 KB Output is correct
74 Correct 1835 ms 7156 KB Output is correct
75 Correct 1866 ms 7304 KB Output is correct
76 Correct 1579 ms 7132 KB Output is correct
77 Correct 1603 ms 7204 KB Output is correct
78 Correct 1539 ms 7156 KB Output is correct
79 Correct 1413 ms 6516 KB Output is correct
80 Correct 1441 ms 6516 KB Output is correct
81 Correct 1449 ms 6572 KB Output is correct
82 Correct 1311 ms 7284 KB Output is correct
83 Correct 1327 ms 7284 KB Output is correct
84 Correct 1283 ms 7280 KB Output is correct
85 Execution timed out 3077 ms 7864 KB Time limit exceeded
86 Halted 0 ms 0 KB -