답안 #1094935

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1094935 2024-09-30T22:27:15 Z raphaelp Make them Meet (EGOI24_makethemmeet) C++14
100 / 100
8 ms 1112 KB
#include <bits/stdc++.h>
using namespace std;
class UnionFind
{
private:
    vector<int> p;

public:
    UnionFind(int x)
    {
        p.assign(x, 0);
        for (int i = 0; i < x; i++)
            p[i] = i;
    }
    int find(int x) { return (x == p[x]) ? x : p[x] = find(p[x]); }
    bool merge(int a, int b)
    {
        int x = find(a), y = find(b);
        if (x == y)
            return 0;
        p[x] = y;
        return 1;
    }
};
void dfs(int x, int p, vector<vector<int>> &AR, vector<vector<int>> &ans, int &cur, int &last)
{
    if (x != last)
    {
        ans[cur][x] = last;
        cur++;
        last = x;
    }
    for (int i = 0; i < AR[x].size(); i++)
    {
        if (AR[x][i] == p)
            continue;
        dfs(AR[x][i], x, AR, ans, cur, last);
        ans[cur][x] = last;
        cur++;
        last = x;
    }
}
int dfs2(int x, int p, vector<vector<int>> &AR, vector<vector<int>> &ans, int &cur, int depth, int add)
{
    int sub = 1, pos = cur - depth;
    cur += add;
    for (int i = 0; i < AR[x].size(); i++)
    {
        if (AR[x][i] == p)
            continue;
        sub += dfs2(AR[x][i], x, AR, ans, cur, depth + 1, add);
    }
    for (int i = 0; i < sub; i++)
    {
        ans[pos][x] = p;
        pos += add;
    }
    return sub;
}
void dfs3(int x, vector<int> &occ, vector<vector<int>> &AR, int &num, int nono)
{
    num++;
    occ[x] = 1;
    for (int i = 0; i < AR[x].size(); i++)
    {
        if (occ[AR[x][i]])
            continue;
        if (AR[x][i] == nono)
            continue;
        dfs3(AR[x][i], occ, AR, num, nono);
    }
}

int main()
{
    int N, M;
    cin >> N >> M;
    vector<vector<int>> AR(N), AR2(N);
    UnionFind UF(N);
    for (int i = 0; i < M; i++)
    {
        int a, b;
        cin >> a >> b;
        AR2[a].push_back(b);
        AR2[b].push_back(a);
    }
    int centre = -1, leaf = -1;
    vector<int> banned(N);
    for (int i = 0; i < N; i++)
    {
        if (centre != -1)
            break;
        for (int j = 0; j < N; j++)
        {
            if (centre != -1)
                break;
            vector<int> temp(N);
            for (int k = 0; k < AR2[i].size(); k++)
                temp[AR2[i][k]] = 1;
            for (int k = 0; k < AR2[j].size(); k++)
                temp[AR2[j][k]] = 0;
            if (temp[j] == 0)
                continue;
            for (int k = 0; k < N; k++)
            {
                if (temp[k] && k != j && k != i)
                {
                    centre = i;
                    leaf = j;
                }
            }
        }
    }

    if (centre == -1)
    {
        int N2 = N;
        if (N % 2 == 0)
            N2++;
        vector<int> perm(N2), C(N2);
        for (int i = 0; i < N2; i++)
        {
            perm[i] = i;
            if (i <= N2 / 2)
            {
                C[i] = i;
                C[N2 - i - 1] = i;
            }
        }
        cout << 2 * N2 << endl;
        for (int i = 0; i < N2; i++)
        {
            for (int k = 0; k < 2; k++)
            {
                for (int j = 0; j < N; j++)
                {
                    cout << C[perm[j]] << ' ';
                }
                cout << '\n';
            }
            swap(perm[0], perm[N2 - 1]);
            for (int j = 1; j < N2 - 1; j++)
                swap(perm[j], perm[j - 1]);
        }
        return 0;
    }
    UF.merge(centre, leaf);
    AR[centre].push_back(leaf);
    AR[leaf].push_back(centre);
    for (int j = 0; j < AR2[leaf].size(); j++)
        banned[AR2[leaf][j]] = 1;
    for (int i = 0; i < AR2[centre].size(); i++)
    {
        if (AR2[centre][i] == leaf)
            continue;
        if (banned[AR2[centre][i]])
            continue;
        if (UF.merge(centre, AR2[centre][i]))
        {
            AR[centre].push_back(AR2[centre][i]);
            AR[AR2[centre][i]].push_back(centre);
            for (int j = 0; j < AR2[AR2[centre][i]].size(); j++)
                banned[AR2[AR2[centre][i]][j]] = 1;
        }
    }
    for (int i = 0; i < N; i++)
    {
        if (i == centre)
            continue;
        for (int j = 0; j < AR2[i].size(); j++)
        {
            if (AR2[i][j] == centre)
                continue;
            if (UF.merge(i, AR2[i][j]))
            {
                AR[i].push_back(AR2[i][j]);
                AR[AR2[i][j]].push_back(i);
            }
        }
    }
    vector<vector<int>> ans(600, vector<int>(N));
    for (int i = 0; i < 600; i++)
        for (int j = 0; j < N; j++)
            ans[i][j] = j;
    for (int i = 0; i < 200; i++)
    {
        ans[i * 2][AR[centre][i % 2]] = centre;
    }
    for (int i = 0; i < AR[centre].size(); i++)
    {
        int pos = 1;
        if (i == 0)
            pos += 2;
        int temp = dfs2(AR[centre][i], centre, AR, ans, pos, 0, (i <= 1) ? 4 : 2);
    }
    for (int i = 0; i < AR[centre].size(); i++)
    {
        for (int j = 1; j < 400; j += 2)
            ans[j][AR[centre][i]] = centre;
    }
    int cur = 400, last = centre;
    dfs(centre, centre, AR, ans, cur, last);
    cout << 600 << endl;
    for (int i = 0; i < 600; i++)
    {
        for (int j = 0; j < N; j++)
        {
            cout << ans[i][j] << ' ';
        }
        cout << endl;
    }
}

Compilation message

Main.cpp: In function 'void dfs(int, int, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&, int&, int&)':
Main.cpp:33:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     for (int i = 0; i < AR[x].size(); i++)
      |                     ~~^~~~~~~~~~~~~~
Main.cpp: In function 'int dfs2(int, int, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&, int&, int, int)':
Main.cpp:47:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |     for (int i = 0; i < AR[x].size(); i++)
      |                     ~~^~~~~~~~~~~~~~
Main.cpp: In function 'void dfs3(int, std::vector<int>&, std::vector<std::vector<int> >&, int&, int)':
Main.cpp:64:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |     for (int i = 0; i < AR[x].size(); i++)
      |                     ~~^~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:98:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |             for (int k = 0; k < AR2[i].size(); k++)
      |                             ~~^~~~~~~~~~~~~~~
Main.cpp:100:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |             for (int k = 0; k < AR2[j].size(); k++)
      |                             ~~^~~~~~~~~~~~~~~
Main.cpp:150:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  150 |     for (int j = 0; j < AR2[leaf].size(); j++)
      |                     ~~^~~~~~~~~~~~~~~~~~
Main.cpp:152:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  152 |     for (int i = 0; i < AR2[centre].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:162:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |             for (int j = 0; j < AR2[AR2[centre][i]].size(); j++)
      |                             ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:170:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  170 |         for (int j = 0; j < AR2[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
Main.cpp:189:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  189 |     for (int i = 0; i < AR[centre].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~~
Main.cpp:194:13: warning: unused variable 'temp' [-Wunused-variable]
  194 |         int temp = dfs2(AR[centre][i], centre, AR, ans, pos, 0, (i <= 1) ? 4 : 2);
      |             ^~~~
Main.cpp:196:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  196 |     for (int i = 0; i < AR[centre].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 8 ms 860 KB Output is correct
5 Correct 5 ms 816 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 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 4 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 512 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 3 ms 600 KB Output is correct
5 Correct 5 ms 860 KB Output is correct
6 Correct 8 ms 860 KB Output is correct
7 Correct 3 ms 604 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 1 ms 348 KB Output is correct
4 Correct 8 ms 860 KB Output is correct
5 Correct 5 ms 816 KB Output is correct
6 Correct 0 ms 512 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 3 ms 600 KB Output is correct
10 Correct 5 ms 860 KB Output is correct
11 Correct 8 ms 860 KB Output is correct
12 Correct 3 ms 604 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 4 ms 828 KB Output is correct
17 Correct 4 ms 860 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
21 Correct 4 ms 788 KB Output is correct
22 Correct 4 ms 860 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 2 ms 344 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 5 ms 836 KB Output is correct
28 Correct 6 ms 752 KB Output is correct
29 Correct 4 ms 856 KB Output is correct
30 Correct 4 ms 692 KB Output is correct
31 Correct 4 ms 688 KB Output is correct
32 Correct 4 ms 856 KB Output is correct
33 Correct 4 ms 856 KB Output is correct
34 Correct 4 ms 856 KB Output is correct
35 Correct 4 ms 860 KB Output is correct
36 Correct 5 ms 692 KB Output is correct
37 Correct 7 ms 860 KB Output is correct
38 Correct 4 ms 860 KB Output is correct
39 Correct 4 ms 860 KB Output is correct
40 Correct 4 ms 716 KB Output is correct
41 Correct 4 ms 860 KB Output is correct
42 Correct 4 ms 692 KB Output is correct
43 Correct 4 ms 856 KB Output is correct
44 Correct 4 ms 644 KB Output is correct
45 Correct 4 ms 860 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 2 ms 348 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 1 ms 344 KB Output is correct
61 Correct 2 ms 348 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 1 ms 436 KB Output is correct
65 Correct 1 ms 348 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 1 ms 348 KB Output is correct
4 Correct 8 ms 860 KB Output is correct
5 Correct 5 ms 816 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 4 ms 348 KB Output is correct
15 Correct 0 ms 512 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 3 ms 600 KB Output is correct
19 Correct 5 ms 860 KB Output is correct
20 Correct 8 ms 860 KB Output is correct
21 Correct 3 ms 604 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 4 ms 828 KB Output is correct
26 Correct 4 ms 860 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 3 ms 604 KB Output is correct
30 Correct 4 ms 788 KB Output is correct
31 Correct 4 ms 860 KB Output is correct
32 Correct 3 ms 604 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 2 ms 344 KB Output is correct
35 Correct 2 ms 348 KB Output is correct
36 Correct 5 ms 836 KB Output is correct
37 Correct 6 ms 752 KB Output is correct
38 Correct 4 ms 856 KB Output is correct
39 Correct 4 ms 692 KB Output is correct
40 Correct 4 ms 688 KB Output is correct
41 Correct 4 ms 856 KB Output is correct
42 Correct 4 ms 856 KB Output is correct
43 Correct 4 ms 856 KB Output is correct
44 Correct 4 ms 860 KB Output is correct
45 Correct 5 ms 692 KB Output is correct
46 Correct 7 ms 860 KB Output is correct
47 Correct 4 ms 860 KB Output is correct
48 Correct 4 ms 860 KB Output is correct
49 Correct 4 ms 716 KB Output is correct
50 Correct 4 ms 860 KB Output is correct
51 Correct 4 ms 692 KB Output is correct
52 Correct 4 ms 856 KB Output is correct
53 Correct 4 ms 644 KB Output is correct
54 Correct 4 ms 860 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 1 ms 348 KB Output is correct
62 Correct 2 ms 348 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 1 ms 348 KB Output is correct
66 Correct 1 ms 348 KB Output is correct
67 Correct 1 ms 348 KB Output is correct
68 Correct 1 ms 348 KB Output is correct
69 Correct 1 ms 344 KB Output is correct
70 Correct 2 ms 348 KB Output is correct
71 Correct 1 ms 348 KB Output is correct
72 Correct 1 ms 348 KB Output is correct
73 Correct 1 ms 436 KB Output is correct
74 Correct 1 ms 348 KB Output is correct
75 Correct 0 ms 348 KB Output is correct
76 Correct 1 ms 348 KB Output is correct
77 Correct 1 ms 348 KB Output is correct
78 Correct 4 ms 860 KB Output is correct
79 Correct 4 ms 856 KB Output is correct
80 Correct 0 ms 348 KB Output is correct
81 Correct 0 ms 348 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 1 ms 348 KB Output is correct
84 Correct 1 ms 348 KB Output is correct
85 Correct 1 ms 348 KB Output is correct
86 Correct 1 ms 348 KB Output is correct
87 Correct 4 ms 348 KB Output is correct
88 Correct 1 ms 348 KB Output is correct
89 Correct 1 ms 348 KB Output is correct
90 Correct 2 ms 660 KB Output is correct
91 Correct 4 ms 860 KB Output is correct
92 Correct 4 ms 724 KB Output is correct
93 Correct 3 ms 604 KB Output is correct
94 Correct 1 ms 348 KB Output is correct
95 Correct 1 ms 360 KB Output is correct
96 Correct 2 ms 344 KB Output is correct
97 Correct 4 ms 688 KB Output is correct
98 Correct 4 ms 860 KB Output is correct
99 Correct 4 ms 688 KB Output is correct
100 Correct 4 ms 860 KB Output is correct
101 Correct 4 ms 856 KB Output is correct
102 Correct 4 ms 860 KB Output is correct
103 Correct 4 ms 644 KB Output is correct
104 Correct 5 ms 856 KB Output is correct
105 Correct 4 ms 860 KB Output is correct
106 Correct 4 ms 860 KB Output is correct
107 Correct 4 ms 812 KB Output is correct
108 Correct 5 ms 860 KB Output is correct
109 Correct 4 ms 856 KB Output is correct
110 Correct 4 ms 820 KB Output is correct
111 Correct 4 ms 856 KB Output is correct
112 Correct 5 ms 804 KB Output is correct
113 Correct 4 ms 864 KB Output is correct
114 Correct 4 ms 864 KB Output is correct
115 Correct 5 ms 860 KB Output is correct
116 Correct 1 ms 352 KB Output is correct
117 Correct 1 ms 352 KB Output is correct
118 Correct 1 ms 352 KB Output is correct
119 Correct 1 ms 352 KB Output is correct
120 Correct 1 ms 360 KB Output is correct
121 Correct 1 ms 348 KB Output is correct
122 Correct 1 ms 348 KB Output is correct
123 Correct 1 ms 348 KB Output is correct
124 Correct 2 ms 348 KB Output is correct
125 Correct 1 ms 348 KB Output is correct
126 Correct 1 ms 348 KB Output is correct
127 Correct 1 ms 348 KB Output is correct
128 Correct 1 ms 348 KB Output is correct
129 Correct 1 ms 348 KB Output is correct
130 Correct 1 ms 344 KB Output is correct
131 Correct 1 ms 348 KB Output is correct
132 Correct 1 ms 348 KB Output is correct
133 Correct 1 ms 348 KB Output is correct
134 Correct 1 ms 348 KB Output is correct
135 Correct 1 ms 348 KB Output is correct
136 Correct 1 ms 344 KB Output is correct
137 Correct 2 ms 604 KB Output is correct
138 Correct 1 ms 348 KB Output is correct
139 Correct 1 ms 348 KB Output is correct
140 Correct 4 ms 856 KB Output is correct
141 Correct 1 ms 348 KB Output is correct
142 Correct 5 ms 860 KB Output is correct
143 Correct 5 ms 860 KB Output is correct
144 Correct 5 ms 856 KB Output is correct
145 Correct 5 ms 860 KB Output is correct
146 Correct 5 ms 856 KB Output is correct
147 Correct 4 ms 860 KB Output is correct
148 Correct 5 ms 796 KB Output is correct
149 Correct 5 ms 776 KB Output is correct
150 Correct 5 ms 856 KB Output is correct
151 Correct 5 ms 860 KB Output is correct
152 Correct 4 ms 860 KB Output is correct
153 Correct 4 ms 860 KB Output is correct
154 Correct 6 ms 860 KB Output is correct
155 Correct 4 ms 860 KB Output is correct
156 Correct 4 ms 860 KB Output is correct
157 Correct 4 ms 860 KB Output is correct
158 Correct 4 ms 860 KB Output is correct
159 Correct 4 ms 860 KB Output is correct
160 Correct 6 ms 1112 KB Output is correct
161 Correct 4 ms 860 KB Output is correct
162 Correct 0 ms 348 KB Output is correct
163 Correct 1 ms 348 KB Output is correct
164 Correct 1 ms 344 KB Output is correct
165 Correct 0 ms 348 KB Output is correct