Submission #1094922

#TimeUsernameProblemLanguageResultExecution timeMemory
1094922raphaelpMake them Meet (EGOI24_makethemmeet)C++14
70 / 100
6 ms1112 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...