답안 #952792

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
952792 2024-03-24T19:54:20 Z emad234 슈퍼트리 잇기 (IOI20_supertrees) C++17
96 / 100
491 ms 73232 KB
#include "supertrees.h"
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define pii pair<ll, ll>
const ll mod = 1e9 + 7;
const ll mxN = 1e6 + 5;
using namespace std;

int dsu[mxN];
int find(int x) { return dsu[x] == x ? x : dsu[x] = find(dsu[x]); }
void merge(int a, int b) { dsu[find(b)] = find(a); }
int construct(std::vector<std::vector<int>> p)
{
	vector<vector<int>> b;
	int n = p.size(), m = p.size();
	b.resize(n);
	for (int i = 0; i < n; i++)
		dsu[i] = i;
	for (int i = 0; i < n; i++)
		b[i].resize(m);
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (i == j)
				continue;
			if (p[i][j] == 1)
			{
				if (find(i) != find(j))
				{
					merge(i, j);
					b[i][j] = 1;
					b[j][i] = 1;
				}
			}
		}
	}
	vector<vector<int>> ty;
	vector<set<int>> v, v1;
	v.resize(n);
	v1.resize(n);
	ty.resize(n);
	for (int i = 0; i < n; i++)
	{
		ty[i].resize(n);
		for (int j = 0; j < m; j++)
		{
			ty[i][j] = -1;
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (i == j)
				continue;
			if (find(i) == find(j))
			{
				if (p[i][j] != 1)
					return 0;
			}
			else
			{
				if (ty[find(i)][find(j)] != -1 && p[i][j] != ty[find(i)][find(j)])
					return 0;
				if (p[i][j] == 2)
				{
					v[find(i)].insert(find(j));
					v[find(j)].insert(find(i));
				}
				if (p[i][j] == 3)
				{
					v1[find(i)].insert(find(j));
					v1[find(j)].insert(find(i));
				}
				ty[find(i)][find(j)] = p[i][j];
			}
		}
	}
	vector<bool> vis(n);
	for (int i = 0; i < n; i++)
	{
		vector<bool> fnd(n);
		int prv = -1;
		if (vis[i])
			continue;
		if (v[i].size() == 1)
			return 0;
		v[i].insert(i);
		for (auto x : v[i])
		{
			fnd[x] = 1;
			for (auto y : v[x])
			{
				fnd[y] = 1;
			}
		}
		for (auto x : v[i])
		{
			for (int j = 0; j < n; j++)
			{
				if (x == j)
					continue;
				if (fnd[j] && p[x][j] != 2)
					return 0;
			}
			vis[x] = 1;
			if (prv != -1)
			{
				b[x][prv] = 1;
				b[prv][x] = 1;
			}
			prv = x;
		}
		if (v[i].size() > 2)
		{
			b[*v[i].begin()][*v[i].rbegin()] = 1;
			b[*v[i].rbegin()][*v[i].begin()] = 1;
		}
	}
	for (int i = 0; i < n; i++)
		vis[i] = 0;
	for (int i = 0; i < n; i++)
	{
		vector<bool> fnd(n);
		int prv = -1;
		if (vis[i])
			continue;
		if (v1[i].size() == 2 || v1[i].size() == 1)
			return 0;
		v1[i].insert(i);
		for (auto x : v1[i])
		{
			fnd[x] = 1;
			for (auto y : v1[x])
			{
				fnd[y] = 1;
			}
		}
		int bg;
		int ed = *v1[i].begin();
		bg = ed;
		ed++;
		ed++;
		for (auto x : v1[i])
		{
			for (int j = 0; j < n; j++)
			{
				if (x == j)
					continue;
				if (fnd[j] && p[x][j] != 3)
					return 0;
			}
			vis[x] = 1;
			if (prv != -1)
			{
				b[x][prv] = 1;
				b[prv][x] = 1;
			}
			prv = x;
		}
		if (v1[i].size() > 3)
		{
			b[*v1[i].begin()][*v1[i].rbegin()] = 1;
			b[*v1[i].rbegin()][*v1[i].begin()] = 1;
			b[bg][ed] = 1;
			b[ed][bg] = 1;
		}
	}
	build(b);
	return 1;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1372 KB Output is correct
7 Correct 168 ms 26300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1372 KB Output is correct
7 Correct 168 ms 26300 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 168 ms 26176 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 1088 KB Output is correct
17 Correct 93 ms 16148 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 42 ms 6996 KB Output is correct
21 Correct 165 ms 26144 KB Output is correct
22 Correct 165 ms 26196 KB Output is correct
23 Correct 173 ms 26264 KB Output is correct
24 Correct 160 ms 26196 KB Output is correct
25 Correct 65 ms 16208 KB Output is correct
26 Correct 65 ms 16144 KB Output is correct
27 Correct 169 ms 26236 KB Output is correct
28 Correct 171 ms 26192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 7 ms 1372 KB Output is correct
9 Correct 161 ms 26248 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 12 ms 3164 KB Output is correct
13 Correct 491 ms 73188 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 6 ms 1884 KB Output is correct
17 Correct 268 ms 39696 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 51 ms 7252 KB Output is correct
22 Correct 178 ms 30736 KB Output is correct
23 Correct 165 ms 26716 KB Output is correct
24 Correct 295 ms 49516 KB Output is correct
25 Correct 75 ms 16400 KB Output is correct
26 Correct 87 ms 20816 KB Output is correct
27 Correct 165 ms 26448 KB Output is correct
28 Correct 293 ms 49488 KB Output is correct
29 Correct 73 ms 16404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 45 ms 6908 KB Output is correct
5 Correct 180 ms 26284 KB Output is correct
6 Correct 172 ms 26156 KB Output is correct
7 Correct 169 ms 26192 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 41 ms 7088 KB Output is correct
10 Correct 188 ms 30804 KB Output is correct
11 Correct 161 ms 26340 KB Output is correct
12 Correct 312 ms 49976 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 41 ms 6996 KB Output is correct
17 Correct 181 ms 30904 KB Output is correct
18 Correct 179 ms 27984 KB Output is correct
19 Correct 174 ms 26900 KB Output is correct
20 Correct 160 ms 26148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1372 KB Output is correct
7 Correct 168 ms 26300 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 168 ms 26176 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 1088 KB Output is correct
17 Correct 93 ms 16148 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 42 ms 6996 KB Output is correct
21 Correct 165 ms 26144 KB Output is correct
22 Correct 165 ms 26196 KB Output is correct
23 Correct 173 ms 26264 KB Output is correct
24 Correct 160 ms 26196 KB Output is correct
25 Correct 65 ms 16208 KB Output is correct
26 Correct 65 ms 16144 KB Output is correct
27 Correct 169 ms 26236 KB Output is correct
28 Correct 171 ms 26192 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 7 ms 1372 KB Output is correct
37 Correct 161 ms 26248 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 12 ms 3164 KB Output is correct
41 Correct 491 ms 73188 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 6 ms 1884 KB Output is correct
45 Correct 268 ms 39696 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 51 ms 7252 KB Output is correct
50 Correct 178 ms 30736 KB Output is correct
51 Correct 165 ms 26716 KB Output is correct
52 Correct 295 ms 49516 KB Output is correct
53 Correct 75 ms 16400 KB Output is correct
54 Correct 87 ms 20816 KB Output is correct
55 Correct 165 ms 26448 KB Output is correct
56 Correct 293 ms 49488 KB Output is correct
57 Correct 73 ms 16404 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 600 KB Output is correct
60 Correct 4 ms 1112 KB Output is correct
61 Correct 73 ms 16212 KB Output is correct
62 Correct 0 ms 600 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 41 ms 7020 KB Output is correct
66 Correct 67 ms 16152 KB Output is correct
67 Correct 70 ms 16160 KB Output is correct
68 Correct 66 ms 16160 KB Output is correct
69 Correct 70 ms 16208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1372 KB Output is correct
7 Correct 168 ms 26300 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 168 ms 26176 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 1088 KB Output is correct
17 Correct 93 ms 16148 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 42 ms 6996 KB Output is correct
21 Correct 165 ms 26144 KB Output is correct
22 Correct 165 ms 26196 KB Output is correct
23 Correct 173 ms 26264 KB Output is correct
24 Correct 160 ms 26196 KB Output is correct
25 Correct 65 ms 16208 KB Output is correct
26 Correct 65 ms 16144 KB Output is correct
27 Correct 169 ms 26236 KB Output is correct
28 Correct 171 ms 26192 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 7 ms 1372 KB Output is correct
37 Correct 161 ms 26248 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 12 ms 3164 KB Output is correct
41 Correct 491 ms 73188 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 6 ms 1884 KB Output is correct
45 Correct 268 ms 39696 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 51 ms 7252 KB Output is correct
50 Correct 178 ms 30736 KB Output is correct
51 Correct 165 ms 26716 KB Output is correct
52 Correct 295 ms 49516 KB Output is correct
53 Correct 75 ms 16400 KB Output is correct
54 Correct 87 ms 20816 KB Output is correct
55 Correct 165 ms 26448 KB Output is correct
56 Correct 293 ms 49488 KB Output is correct
57 Correct 73 ms 16404 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 45 ms 6908 KB Output is correct
62 Correct 180 ms 26284 KB Output is correct
63 Correct 172 ms 26156 KB Output is correct
64 Correct 169 ms 26192 KB Output is correct
65 Correct 0 ms 348 KB Output is correct
66 Correct 41 ms 7088 KB Output is correct
67 Correct 188 ms 30804 KB Output is correct
68 Correct 161 ms 26340 KB Output is correct
69 Correct 312 ms 49976 KB Output is correct
70 Correct 1 ms 344 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 41 ms 6996 KB Output is correct
74 Correct 181 ms 30904 KB Output is correct
75 Correct 179 ms 27984 KB Output is correct
76 Correct 174 ms 26900 KB Output is correct
77 Correct 160 ms 26148 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 600 KB Output is correct
80 Correct 4 ms 1112 KB Output is correct
81 Correct 73 ms 16212 KB Output is correct
82 Correct 0 ms 600 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 1 ms 348 KB Output is correct
85 Correct 41 ms 7020 KB Output is correct
86 Correct 67 ms 16152 KB Output is correct
87 Correct 70 ms 16160 KB Output is correct
88 Correct 66 ms 16160 KB Output is correct
89 Correct 70 ms 16208 KB Output is correct
90 Correct 0 ms 348 KB Output is correct
91 Correct 0 ms 344 KB Output is correct
92 Correct 7 ms 856 KB Output is correct
93 Correct 72 ms 16148 KB Output is correct
94 Correct 1 ms 348 KB Output is correct
95 Correct 1 ms 344 KB Output is correct
96 Correct 0 ms 348 KB Output is correct
97 Correct 18 ms 4444 KB Output is correct
98 Correct 89 ms 21008 KB Output is correct
99 Correct 70 ms 16724 KB Output is correct
100 Correct 70 ms 16208 KB Output is correct
101 Correct 71 ms 16216 KB Output is correct
102 Incorrect 484 ms 73232 KB Answer gives possible 1 while actual possible 0
103 Halted 0 ms 0 KB -