Submission #1094922

# Submission time Handle Problem Language Result Execution time Memory
1094922 2024-09-30T20:58:20 Z raphaelp Make them Meet (EGOI24_makethemmeet) C++14
70 / 100
6 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;
    }
}
void dfs2(int x, int p, vector<vector<int>> &AR, vector<int> &par, vector<int> &deb, int &cur, int depth)
{
    par[x] = p;
    deb[x] = cur - depth;
    cur += 2;
    for (int i = 0; i < AR[x].size(); i++)
    {
        if (AR[x][i] == p)
            continue;
        dfs2(AR[x][i], x, AR, par, deb, cur, depth + 1);
    }
}
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);
    }
}
void dfs4(int x, vector<vector<int>> &AR, vector<int> &occ)
{
    occ[x] = 1;
    for (int i = 0; i < AR[x].size(); i++)
    {
        if (occ[AR[x][i]])
            continue;
        dfs4(AR[x][i], AR, occ);
    }
}
int good(int leaf, int centre, vector<vector<int>> &AR, vector<int> &banned)
{
    for (int i = 0; i < AR[leaf].size(); i++)
    {
        if (AR[leaf][i] == centre)
            break;
        if (i == AR[leaf].size() - 1)
            return 0;
    }
    int N = banned.size();
    banned.assign(N, 0);
    for (int i = 0; i < AR[leaf].size(); i++)
    {
        banned[AR[leaf][i]] = 1;
    }
    vector<int> occ(N, 0);
    occ[leaf] = 1;
    occ[centre] = 1;
    for (int i = 0; i < AR[centre].size(); i++)
    {
        if (AR[centre][i] == leaf)
            continue;
        if (banned[AR[centre][i]])
            continue;
        dfs4(AR[centre][i], AR, occ);
    }
    for (int i = 0; i < N; i++)
        if (occ[i] == 0)
            return 0;
    return 1;
}
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 leaf = -1, centre = -1;
    vector<int> banned(N);
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (i == j)
                continue;
            if (good(i, j, AR2, banned))
            {
                leaf = i;
                centre = j;
                break;
            }
        }
        if (leaf != -1)
            break;
    }

    if (leaf == -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(leaf, centre);
    AR[leaf].push_back(centre);
    AR[centre].push_back(leaf);
    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 || i == leaf)
            continue;
        for (int j = 0; j < AR2[i].size(); j++)
        {
            if (AR2[i][j] == centre || AR2[i][j] == leaf)
                continue;
            if (UF.merge(i, AR2[i][j]))
            {
                AR[i].push_back(AR2[i][j]);
                AR[AR2[i][j]].push_back(i);
            }
        }
    }
    vector<int> par(N), deb(N, 1000);
    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 < 400; i++)
    {
        ans[i][leaf] = centre;
    }
    for (int i = 0; i < AR[centre].size(); i++)
    {
        if (AR[centre][i] == leaf)
            continue;
        int pos = 0;
        dfs2(AR[centre][i], centre, AR, par, deb, pos, 0);
    }
    for (int i = 0; i < N; i++)
    {
        if (i == leaf || i == centre)
            continue;
        for (int j = deb[i]; j < 400; j += 2)
        {
            ans[j][i] = par[i];
        }
    }
    int cur = 400, last = leaf;
    dfs(leaf, leaf, 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 'void dfs2(int, int, std::vector<std::vector<int> >&, std::vector<int>&, std::vector<int>&, int&, int)':
Main.cpp:48:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |     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:59:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |     for (int i = 0; i < AR[x].size(); i++)
      |                     ~~^~~~~~~~~~~~~~
Main.cpp: In function 'void dfs4(int, std::vector<std::vector<int> >&, std::vector<int>&)':
Main.cpp:71:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |     for (int i = 0; i < AR[x].size(); i++)
      |                     ~~^~~~~~~~~~~~~~
Main.cpp: In function 'int good(int, int, std::vector<std::vector<int> >&, std::vector<int>&)':
Main.cpp:80:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |     for (int i = 0; i < AR[leaf].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
Main.cpp:84:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |         if (i == AR[leaf].size() - 1)
      |             ~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |     for (int i = 0; i < AR[leaf].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
Main.cpp:96:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |     for (int i = 0; i < AR[centre].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:177:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  177 |     for (int i = 0; i < AR2[centre].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:187:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  187 |             for (int j = 0; j < AR2[AR2[centre][i]].size(); j++)
      |                             ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:195:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  195 |         for (int j = 0; j < AR2[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
Main.cpp:215:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  215 |     for (int i = 0; i < AR[centre].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 4 ms 780 KB Output is correct
5 Correct 4 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 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 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 5 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 4 ms 836 KB Output is correct
6 Correct 4 ms 860 KB Output is correct
7 Correct 4 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 4 ms 780 KB Output is correct
5 Correct 4 ms 860 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 2 ms 604 KB Output is correct
10 Correct 4 ms 836 KB Output is correct
11 Correct 4 ms 860 KB Output is correct
12 Correct 4 ms 860 KB Output is correct
13 Correct 1 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 860 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 348 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 4 ms 736 KB Output is correct
22 Correct 5 ms 856 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 1 ms 436 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 4 ms 860 KB Output is correct
28 Correct 4 ms 788 KB Output is correct
29 Correct 4 ms 860 KB Output is correct
30 Correct 4 ms 856 KB Output is correct
31 Correct 4 ms 860 KB Output is correct
32 Correct 4 ms 860 KB Output is correct
33 Correct 4 ms 856 KB Output is correct
34 Correct 5 ms 856 KB Output is correct
35 Correct 4 ms 860 KB Output is correct
36 Correct 4 ms 880 KB Output is correct
37 Correct 4 ms 860 KB Output is correct
38 Correct 5 ms 860 KB Output is correct
39 Correct 4 ms 860 KB Output is correct
40 Correct 4 ms 860 KB Output is correct
41 Correct 4 ms 860 KB Output is correct
42 Correct 4 ms 860 KB Output is correct
43 Correct 4 ms 856 KB Output is correct
44 Correct 4 ms 860 KB Output is correct
45 Correct 4 ms 764 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 344 KB Output is correct
49 Correct 1 ms 436 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 1 ms 344 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 2 ms 344 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 2 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 488 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 344 KB Output is correct
65 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 4 ms 780 KB Output is correct
5 Correct 4 ms 860 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 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 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 5 ms 604 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 4 ms 836 KB Output is correct
20 Correct 4 ms 860 KB Output is correct
21 Correct 4 ms 860 KB Output is correct
22 Correct 1 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 860 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 348 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
30 Correct 4 ms 736 KB Output is correct
31 Correct 5 ms 856 KB Output is correct
32 Correct 3 ms 604 KB Output is correct
33 Correct 1 ms 436 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 4 ms 860 KB Output is correct
37 Correct 4 ms 788 KB Output is correct
38 Correct 4 ms 860 KB Output is correct
39 Correct 4 ms 856 KB Output is correct
40 Correct 4 ms 860 KB Output is correct
41 Correct 4 ms 860 KB Output is correct
42 Correct 4 ms 856 KB Output is correct
43 Correct 5 ms 856 KB Output is correct
44 Correct 4 ms 860 KB Output is correct
45 Correct 4 ms 880 KB Output is correct
46 Correct 4 ms 860 KB Output is correct
47 Correct 5 ms 860 KB Output is correct
48 Correct 4 ms 860 KB Output is correct
49 Correct 4 ms 860 KB Output is correct
50 Correct 4 ms 860 KB Output is correct
51 Correct 4 ms 860 KB Output is correct
52 Correct 4 ms 856 KB Output is correct
53 Correct 4 ms 860 KB Output is correct
54 Correct 4 ms 764 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 344 KB Output is correct
58 Correct 1 ms 436 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 1 ms 344 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 2 ms 344 KB Output is correct
65 Correct 1 ms 348 KB Output is correct
66 Correct 2 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 348 KB Output is correct
70 Correct 1 ms 488 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 344 KB Output is correct
74 Correct 1 ms 600 KB Output is correct
75 Correct 1 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 344 KB Output is correct
81 Correct 0 ms 344 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 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 5 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 604 KB Output is correct
91 Correct 4 ms 856 KB Output is correct
92 Correct 4 ms 860 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 348 KB Output is correct
96 Correct 1 ms 348 KB Output is correct
97 Correct 4 ms 860 KB Output is correct
98 Correct 4 ms 768 KB Output is correct
99 Correct 4 ms 860 KB Output is correct
100 Correct 4 ms 860 KB Output is correct
101 Correct 6 ms 764 KB Output is correct
102 Correct 4 ms 860 KB Output is correct
103 Correct 4 ms 856 KB Output is correct
104 Correct 4 ms 860 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 688 KB Output is correct
108 Correct 4 ms 860 KB Output is correct
109 Correct 4 ms 860 KB Output is correct
110 Correct 4 ms 860 KB Output is correct
111 Correct 4 ms 860 KB Output is correct
112 Correct 5 ms 1112 KB Output is correct
113 Correct 4 ms 860 KB Output is correct
114 Correct 4 ms 864 KB Output is correct
115 Correct 4 ms 864 KB Output is correct
116 Correct 1 ms 348 KB Output is correct
117 Correct 1 ms 440 KB Output is correct
118 Correct 1 ms 348 KB Output is correct
119 Correct 1 ms 348 KB Output is correct
120 Correct 1 ms 348 KB Output is correct
121 Correct 1 ms 348 KB Output is correct
122 Correct 2 ms 348 KB Output is correct
123 Correct 1 ms 348 KB Output is correct
124 Correct 1 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 496 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 348 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 344 KB Output is correct
135 Correct 1 ms 348 KB Output is correct
136 Correct 1 ms 348 KB Output is correct
137 Incorrect 1 ms 348 KB If people start at 1 and 5, then they can avoid each other
138 Halted 0 ms 0 KB -