Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> ad[100000]; int ans[100000]; bool used[100000]; int d[4] = {-2, -1, 1, 2}; int n; bool check() { int i, j, k; memset(used, false, sizeof(used)); used[ans[0]] = true; used[ans[1]] = true; used[ans[2]] = true; used[ans[3]] = true; for (i = 2; i < n - 2; i++) { for (j = 0; j < 4; j++) { if (used[ad[ans[i]][j]]) continue; for (k = 0; k < 3; k++) if (ad[ans[i]][j] == ans[i + d[k]]) break; if (k == 3) { ans[i + 2] = ad[ans[i]][j]; used[ad[ans[i]][j]] = true; break; } } } for (i = 0; i < n; i++) { for (j = 0; j < 4; j++) { for (k = 0; k < 4; k++) { if (ad[ans[i]][j] == ans[(n + i + d[k]) % n]) break; } if (k == 4) { return false; } } } return true; } int main() { int i, j, k, l; scanf("%d", &n); for (i = 0; i < n * 2; i++) { int a, b; scanf("%d %d", &a, &b); a--; b--; ad[a].push_back(b); ad[b].push_back(a); } for (i = 0; i < n; i++) { if (ad[i].size() != 4) { printf("-1\n"); return 0; } } for (i = 0; i < n; i++) ans[i] = 0; ans[0] = 0; for (i = 0; i < 4; i++) { ans[1] = ad[0][i]; for (j = 0; j < 4; j++) { ans[2] = ad[ans[1]][j]; for (k = 0; k < 4; k++) if (ad[ans[2]][k] == 0) break; if (k == 4) continue; for (k = 0; k < 4; k++) { ans[3] = ad[ans[2]][k]; for (l = 0; l < 4; l++) if (ad[ans[3]][l] == ans[1]) break; if (l != 4) { if (check()) { for (l = 0; l < n; l++) printf("%d ", ans[l] + 1); printf("\n"); return 0; } } } } } printf("-1\n"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> adj[100009]; int col[100009], occ[100009]; vector<int> out; int n, u, v; bool solve(int a, int b) { memset(col, (0), sizeof(col)); ; out.clear(); out.push_back((1)); out.push_back((a)); out.push_back((b)); col[1] = col[a] = col[b] = true; for (int i = 2; i < n; i++) { if (i >= ((int)out.size())) break; int f, s; f = out[i - 1]; s = out[i]; for (int j = 0, flag = 1; j < 4 && flag; j++) { for (int k = 0; k < 4; k++) { if (adj[f][j] == adj[s][k] && !col[adj[f][j]]) { col[adj[f][j]] = true; flag = 0; out.push_back((adj[f][j])); break; } } } } if (((int)out.size()) != n) return false; return true; } int main() { cin >> n; for (int i = 0; i < 2 * n; i++) { cin >> u >> v; occ[u]++; occ[v]++; adj[u].push_back((v)); adj[v].push_back((u)); } for (int i = 1; i <= n; i++) { if (occ[i] != 4) { cout << -1 << endl; return 0; } } for (int i = 0, fl = 1; i < 4 && fl; i++) { for (int j = i + 1; j < 4; j++) { if (solve(adj[1][i], adj[1][j])) { fl = 0; break; } if (solve(adj[1][j], adj[1][i])) { fl = 0; break; } } } if (((int)out.size()) != n) { cout << -1 << endl; return 0; } for (int i = 0; i < ((int)out.size()); i++) cout << out[i] << " "; cout << endl; return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
// @author Sanzhar import java.io.*; import java.util.*; public class Template { BufferedReader in; PrintWriter out; StringTokenizer st; String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (Exception e) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } public void run() throws Exception { //in = new BufferedReader(new FileReader("input.txt")); //out = new PrintWriter(new FileWriter("output.txt")); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.flush(); out.close(); in.close(); } public void solve() throws Exception { int n = nextInt(); ArrayList<Integer>[] c = new ArrayList[n + 1]; for (int i = 1; i <= n; i++) { c[i] = new ArrayList<Integer>(); } for (int i = 0; i < 2 * n; i++) { int a = nextInt(); int b = nextInt(); c[a].add(b); c[b].add(a); } for (int i = 1; i <= n; i++) { if (c[i].size() != 4) { out.println(-1); return; } } if (n == 5) { for (int i = 1; i <= n; i++) { out.print(i + " "); } out.println(); return; } if (n == 6) { int[] ans = new int[n]; boolean[] used = new boolean[n + 1]; ans[0] = 1; used[1] = true; for (int i = 2; i <= 6; i++) { if (!c[1].contains(i)) { ans[3] = i; used[i] = true; break; } } for (int i = 1; i <= 6; i++) { if (!used[i]) { ans[1] = i; used[i] = true; break; } } for (int i = 1; i <= 6; i++) { if (!used[i]) { if (!c[ans[1]].contains(i)) { ans[4] = i; used[i] = true; break; } } } for (int i = 1; i <= 6; i++) { if (!used[i]) { ans[2] = i; used[i] = true; break; } } for (int i = 1; i <= 6; i++) { if (!used[i]) { ans[5] = i; used[i] = true; break; } } for (int i = 0; i < n; i++) { out.print(ans[i] + " "); } out.println(); return; } boolean[] used = new boolean[n + 1]; int[] ans = new int[n]; int k = 0; int[] d = new int[4]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (c[1].contains(c[c[1].get(i)].get(j))) { d[i]++; } } } int x1 = 2; int x2 = 2; for (int i = 0; i < 4; i++) { if (d[i] == 1) { x1--; } else { x2--; } } if (x1 != 0 || x2 != 0) { out.println(-1); return; } for (int i = 0; i < 4; i++) { if (d[i] == 1) { ans[k++] = c[1].get(i); used[ans[0]] = true; break; } } for (int i = 0; i < 4; i++) { if (c[1].contains(c[ans[0]].get(i))) { ans[k++] = c[ans[0]].get(i); used[ans[1]] = true; break; } } ans[k++] = 1; used[1] = true; for (int i = 0; i < 4; i++) { if (d[i] == 2 && !used[c[1].get(i)]) { ans[k++] = c[1].get(i); used[ans[3]] = true; } } for (int i = 0; i < 4; i++) { if (d[i] == 1 && !used[c[1].get(i)]) { ans[k++] = c[1].get(i); used[ans[4]] = true; } } int last = ans[3]; boolean ok; while (k < n) { ok = false; for (int i = 0; i < 4; i++) { if (!used[c[last].get(i)]) { ans[k++] = c[last].get(i); used[ans[k - 1]] = true; ok = true; } } if (!ok) { out.println(-1); return; } last = ans[k - 2]; } if (!c[ans[0]].contains(ans[n - 1])) { out.println(-1); return; } if (!c[ans[0]].contains(ans[n - 2])) { out.println(-1); return; } if (!c[ans[1]].contains(ans[n - 1])) { out.println(-1); return; } for (int i = 0; i < n; i++) { out.print(ans[i] + " "); } out.println(); } public static void main(String[] args) throws Exception { new Template().run(); } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const int oo = 1 << 30; const double PI = M_PI; const double EPS = 1e-15; const int MaxV = 100005; int V, E; vector<int> g[MaxV]; vector<int> answer; bool used[MaxV]; int pos[MaxV]; bool find(int node, int x) { for (int i = 0; i < 4; i++) if (g[node][i] == x) return true; return false; } bool run() { do { bool isok = true; for (int i = 1; i < V; i++) if (!find(pos[i - 1], pos[i])) { isok = false; break; } if (!find(pos[0], pos[V - 1])) isok = false; if (isok) { for (int i = 0; i < V; i++) if (!find(pos[(i - 1 + V) % V], pos[(i + 1) % V])) { isok = false; break; } } if (isok) { for (int i = 0; i < V; i++) cout << pos[i] + 1 << " "; return true; } } while (next_permutation(pos, pos + V)); return false; } bool check(vector<int> v) { if ((int)v.size() != V) return false; for (int i = 0; i < V; i++) { if (!find(v[(i - 1 + V) % V], v[i])) return false; if (!find(v[(i + 1 + V) % V], v[i])) return false; if (!find(v[(i - 2 + V) % V], v[i])) return false; if (!find(v[(i + 2 + V) % V], v[i])) return false; } return true; } int main() { cin.sync_with_stdio(false); cin >> V; for (int i = 0; i < 2 * V; i++) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } for (int i = 0; i < V; i++) if (g[i].size() != 4) { cout << -1 << endl; return 0; } if (V <= 10) { for (int i = 0; i < V; i++) pos[i] = i; if (!run()) cout << -1 << endl; return 0; } int u = -1; int v = -1; for (int i = 0; i < 4; i++) for (int j = 0; j < i; j++) { int a = g[0][i]; int b = g[0][j]; if (!find(a, b)) { int share = 0; for (int k = 0; k < 4; k++) if (find(a, g[b][k])) share++; if (share == 1) { u = a; v = b; } } } if (u == -1) { cout << -1 << endl; return 0; } vector<int> d; for (int k = 0; k < 4; k++) { if (g[0][k] == u) continue; if (g[0][k] == v) continue; d.push_back(g[0][k]); } vector<int> answer[2]; for (int k = 0; k < 2; k++) { answer[k].push_back(u); answer[k].push_back(d[k]); answer[k].push_back(0); answer[k].push_back(d[1 - k]); answer[k].push_back(v); used[0] = true; used[u] = true; used[v] = true; used[d[0]] = true; used[d[1]] = true; int current = 3; bool badNode = false; for (int i = 0; i < V - 5; i++) { int nextNode = -1; for (int j = 0; j < 4; j++) if (!used[g[answer[k][current]][j]]) { nextNode = g[answer[k][current]][j]; break; } if (nextNode == -1) { badNode = true; break; } current = current + 1; used[nextNode] = true; answer[k].push_back(nextNode); } memset(used, false, sizeof(used)); } if (check(answer[0])) { for (int i = 0; i < V; i++) cout << answer[0][i] + 1 << " "; return 0; } if (check(answer[1])) { for (int i = 0; i < V; i++) cout << answer[1][i] + 1 << " "; return 0; } cout << -1 << endl; return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> ans, a[200000]; map<int, int> was; int p1, p2, pr, pt; int k; int n, x, y; void dfs() { int sz = ans.size(); int x = ans[sz - 1]; int y = ans[sz - 2]; for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) if (a[x][i] == a[y][j] && !was[a[y][j]]) { was[a[y][j]] = 1; ans.push_back(a[y][j]); if (ans.size() < n) dfs(); else { for (int c = 0; c < ans.size(); c++) cout << ans[c] << " "; exit(0); } ans.pop_back(); was[a[y][j]] = 0; } } int main() { cin >> n; for (int i = 1; i <= n + n; i++) { cin >> x >> y; a[x].push_back(y); a[y].push_back(x); } was[1] = 1; ans.push_back(1); for (int i = 1; i <= n; i++) if (a[i].size() != 4) { cout << -1; return 0; } for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) if (i != j) { x = a[1][i]; y = a[1][j]; was[x] = 1; was[y] = 1; ans.push_back(x); ans.push_back(y); dfs(); ans.pop_back(); ans.pop_back(); was[x] = 0; was[y] = 0; } cout << -1; return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.LinkedList; import java.util.StringTokenizer; public class C { static class Scanner{ BufferedReader br=null; StringTokenizer tk=null; public Scanner(){ br=new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException{ while(tk==null || !tk.hasMoreTokens()) tk=new StringTokenizer(br.readLine()); return tk.nextToken(); } public int nextInt() throws NumberFormatException, IOException{ return Integer.valueOf(next()); } public double nextDouble() throws NumberFormatException, IOException{ return Double.valueOf(next()); } } static LinkedList<Integer>[] g; static boolean[] visited; public static void main(String args[]) throws NumberFormatException, IOException{ Scanner sc=new Scanner(); int N=sc.nextInt(); g=new LinkedList[N]; visited=new boolean[N]; for(int i=0;i<N;i++){ g[i]=new LinkedList<Integer>(); visited[i]=false; } for(int i=0;i<2*N;i++){ int a=sc.nextInt(); int b=sc.nextInt(); a--;b--; g[a].add(b); g[b].add(a); } for(int i=0;i<N;i++){ if (g[i].size()!=4){ System.out.println("-1"); return; } } boolean flag=false; ArrayList<Integer> sol=new ArrayList<Integer>(); for(int i=0;i<4 && !flag;i++){ for(int j=i+1;j<4 && !flag;j++){ int a=g[0].get(i); int b=g[0].get(j); if (g[a].contains(b) && g[b].contains(a)){ sol.clear(); sol.add(0); sol.add(a); sol.add(b); if (simulate(sol,N)){ flag=true; break; } sol.clear(); sol.add(0); sol.add(b); sol.add(a); if (simulate(sol,N)){ flag=true; break; } } } } if (!flag) System.out.println("-1"); else{ for(int e: sol) System.out.print((e+1)+" "); System.out.println(); } } static boolean simulate(ArrayList<Integer> a,int N){ for(int i=0;i<N;i++) visited[i]=false; for(int e: a) visited[e]=true; while(a.size()<N){ int f=a.get(a.size() - 3); int s=a.get(a.size() - 2); int t=a.get(a.size() - 1); boolean found=false; for(int e: g[s]){ if (e!=f && g[t].contains(e)){ if (visited[e]) continue; found=true; a.add(e); visited[e]=true; break; } } if (!found) return false; } return true; } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &o, vector<T> &v) { for (typeof(v.size()) i = 0; i < v.size(); ++i) o << v[i] << " "; o << endl; return o; } int neigh[112345][4]; int deg[112345], a[112345]; int taken[112345]; void isvalid(bool x) { if (!x) { printf("-1\n"); exit(0); } } int trythis(int *a, const int from, const int till) { for (int i = from; i <= till; ++i) { int vac = 0, who; for (int j = 0; j < 4; ++j) if (!taken[neigh[a[i]][j]]) { vac++; who = neigh[a[i]][j]; } if (vac != 1) return 0; taken[a[i + 2] = who] = 1; } return 1; } int main() { int i, j, n; scanf("%d", &n); for (i = 2 * n; i; --i) { int x, y; scanf("%d%d", &x, &y); neigh[x][deg[x]] = y; isvalid((++deg[x]) <= 4); swap(x, y); neigh[x][deg[x]] = y; isvalid((++deg[x]) <= 4); } int done = 0; int *p = neigh[1]; sort(p, p + 4); do { memset(taken, 0, sizeof(taken)); a[1] = p[0]; a[2] = p[1]; a[3] = 1; a[4] = p[2]; a[5] = p[3]; for (i = 1; i <= 5; ++i) { taken[a[i]] = 1; for (j = i + 1; (j <= i + 2) && (j <= 5); ++j) { int succ = 0; for (int k = 0; k < 4; ++k) if (neigh[a[i]][k] == a[j]) succ = 1; if (!succ) break; } if (!((j > i + 2) || (j > 5))) break; } if (i <= 5) continue; if (trythis(a, 4, n - 2)) { done = 1; break; } } while (next_permutation(p, p + 4)); if (done) for (i = 1; i <= n; ++i) printf("%d ", a[i]); else printf("-1"); printf("\n"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int n; vector<int> v[maxn]; vector<int> li[maxn], out; bool vis[maxn]; void dfs(int x) { vis[x] = 1; out.push_back(x); for (int i = 0; i < li[x].size(); i++) { if (!vis[li[x][i]]) dfs(li[x][i]); } } int cnt(int a, int b) { int res = 0; for (int i = 0; i < v[a].size(); i++) { int toit = v[a][i]; for (int j = 0; j < v[b].size(); j++) { if (toit == v[b][j]) { res++; break; } } } return res; } bool sol() { for (int i = 1; i <= n; i++) if (v[i].size() != 4) return 0; for (int i = 1; i <= n; i++) { for (int j = 0; j < v[i].size(); j++) { int toit = v[i][j]; if (cnt(i, toit) == 2) li[i].push_back(toit); } } for (int i = 1; i <= n; i++) if (li[i].size() != 2) return 0; dfs(1); if (out.size() != n) return 0; else { for (int i = 0; i < out.size(); i++) printf("%d ", out[i]); return 1; } } int pai[10]; int di[4] = {-2, -1, 1, 2}; bool check() { for (int i = 1; i <= n; i++) { for (int j = 0; j < 4; j++) { int po = i + di[j] + n; if (po > n) po -= n; if (po > n) po -= n; po = pai[po]; bool finding = 0; for (int k = 0; k < v[pai[i]].size(); k++) { if (v[pai[i]][k] == po) { finding = 1; break; } } if (!finding) return 0; } } return 1; } bool go() { for (int i = 1; i <= n; i++) pai[i] = i; do { if (check()) { for (int i = 1; i <= n; i++) printf("%d%c", pai[i], i == n ? '\n' : ' '); return 1; } } while (next_permutation(pai + 1, pai + n + 1)); return 0; } int main() { scanf("%d", &n); int a, b; for (int i = 0; i < 2 * n; i++) { scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); } if (n > 6) { if (!sol()) puts("-1"); } else { if (!go()) puts("-1"); } }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; static inline bool get(int &v) { int s = 1, c; while (!isdigit(c = getchar()) && c != '-') { if (c == EOF) break; } if (c == EOF) return 0; if (c == '-') s = 0, v = 0; else v = c ^ 48; for (; isdigit(c = getchar()); v = (v << 1) + (v << 3) + (c ^ 48)) ; v = (s ? v : -v); return 1; } vector<int> link[100005]; int cnt[100005]; int vis[100005]; vector<int> ans; int n; int has(vector<int> e, int val) { for (int i = 0; i < e.size(); i++) if (e[i] == val) return 1; return false; } void dfs(int val) { int i; vis[val] = 1; ans.push_back(val); if (ans.size() == n) { for (i = 0; i < n; i++) printf("%d ", ans[i]); printf("\n"); exit(0); } for (i = 0; i < link[val].size(); i++) { if (!vis[link[val][i]] && ans.size() <= 1) { dfs(link[val][i]); vis[link[val][i]] = 0; ans.pop_back(); } else if (!vis[link[val][i]] && ans.size() >= 2) { if (has(link[ans[ans.size() - 2]], ans[ans.size() - 1]) && has(link[ans[ans.size() - 2]], link[val][i]) && has(link[ans[ans.size() - 1]], link[val][i])) { dfs(link[val][i]); vis[link[val][i]] = 0; ans.pop_back(); } } } } int main() { int a, b, i; get(n); memset(cnt, 0, sizeof(cnt)); memset(vis, 0, sizeof(vis)); for (i = 0; i < 2 * n; i++) { get(a); get(b); cnt[a]++; cnt[b]++; link[a].push_back(b); link[b].push_back(a); } int ok = 1; for (i = 1; i <= n; i++) { if (cnt[i] != 4) { ok = 0; break; } } if (!ok) printf("-1\n"); else { dfs(1); printf("-1\n"); } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int n, x, y, h[100005], w[100005]; bool g, f[100005], c; vector<int> vt[100005]; void back(int p, int u, int r) { w[p] = u; f[u] = true; if (p == n - 1) c = true; ; for (int i = 0; i < 4; i++) if (!f[vt[u][i]]) { g = false; for (int j = 0; j < 4; j++) if (vt[r][j] == vt[u][i]) { g = true; break; } if (g) back(p + 1, vt[u][i], u); } } int main() { scanf("%d", &n); for (int i = 1; i <= 2 * n; i++) { scanf("%d %d", &x, &y); vt[x].push_back(y); vt[y].push_back(x); } for (int i = 1; i <= n; i++) if (vt[i].size() != 4) { cout << -1 << endl; return 0; } for (int i = 0; i < 4; i++) for (int j = i + 1; j < 4; j++) { c = false; memset(w, 0, sizeof(w)); memset(f, false, sizeof(f)); w[1] = 1; w[2] = vt[1][i]; w[n] = vt[1][j]; f[w[2]] = true; f[w[n]] = true; f[1] = true; back(2, w[2], 1); if (c) for (int k = 1; k <= n; k++) h[k] = w[k]; } if (h[1] > 0) { for (int i = 1; i < n; i++) cout << h[i] << ' '; cout << h[n] << endl; } else cout << -1 << endl; return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> g[100001], ans; int n; bool used[100001]; void dfs(int v, int p, int d) { used[v] = true; ans.push_back(v); if (++d == n) { if (v == 1) { for (int i = 0; i < ans.size(); ++i) cout << ans[i] << " "; cout << endl; exit(0); } } else for (int i = 0; i < 4; ++i) { int v2 = g[v][i]; if (!used[v2]) { for (int j = 0; j < 4; ++j) { if (g[p][j] == v2 && !used[g[p][j]]) dfs(v2, v, d); } } } ans.pop_back(); used[v] = false; } int main() { cin >> n; for (int i = 0, a, b, k = n << 1; i < k; ++i) { cin >> a >> b; if (a == b) return !printf("-1"); g[a].push_back(b); g[b].push_back(a); } for (int i = 1; i <= n; ++i) if (g[i].size() != 4) return !printf("-1"); for (int i = 0; i < 4; ++i) dfs(g[1][i], 1, 0); return !printf("-1"); }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, U b) { if (a < b) a = b; } template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) { gn(x1), gn(x2); } template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) { gn(x1, x2), gn(x3); } template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) { gn(x1, x2), gn(x3, x4); } template <class T> inline void print(T first) { if (first < 0) { putchar('-'); return print(-first); } if (first < 10) { putchar('0' + first); return; } print(first / 10); putchar(first % 10 + '0'); } template <class T> inline void println(T first) { print(first); putchar('\n'); } template <class T> inline void printsp(T first) { print(first); putchar(' '); } template <class T1, class T2> inline void print(T1 x1, T2 x2) { printsp(x1), println(x2); } template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) { printsp(x1), printsp(x2), println(x3); } template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) { printsp(x1), printsp(x2), printsp(x3), println(x4); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } vector<int> adj[100010], ans; int n, a, b, c; int vst[100010]; void solve() { ans.clear(); memset(vst, 0, sizeof vst); ans.push_back(a); ans.push_back(b); ans.push_back(c); vst[a] = vst[b] = vst[c] = 1; int flag = 1; for (int step = 3; step < n && flag; step++) { flag = 0; for (int i = 0; i < 4 && flag == 0; i++) { int u = adj[b][i]; if (vst[u]) continue; for (int j = 0; j < 4 && flag == 0; j++) { int v = adj[c][j]; if (vst[v]) continue; if (u == v) { flag = 1; ans.push_back(u); vst[u] = 1; a = b, b = c, c = u; } } } } if (flag) { for (int i = 0; i < ans.size(); i++) printsp(ans[i]); exit(0); } } int main() { gn(n); for (int i = 0; i < 2 * n; i++) { int u, v; gn(u, v); adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n; i++) if (adj[i].size() != 4) { puts("-1"); return 0; } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) if (i != j) { a = 1, b = adj[1][i], c = adj[1][j]; solve(); } } puts("-1"); }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int sam(vector<int> a, vector<int> b) { int ret = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (a[i] == b[j]) { ret++; } } } return ret; } vector<int> pat[100000]; vector<int> rin[100000]; int main() { int num; scanf("%d", &num); for (int i = 0; i < num * 2; i++) { int za, zb; scanf("%d%d", &za, &zb); za--; zb--; pat[za].push_back(zb); pat[zb].push_back(za); } for (int i = 0; i < num; i++) { if (pat[i].size() != 4) { printf("-1\n"); return 0; } if (sam(pat[i], pat[i]) != 4) { printf("-1\n"); return 0; } } if (num >= 7) { for (int i = 0; i < num; i++) { for (int j = 0; j < 4; j++) { if (sam(pat[i], pat[pat[i][j]]) == 2) { rin[i].push_back(pat[i][j]); } } if (rin[i].size() != 2) { printf("-1\n"); return 0; } } vector<int> vec; int now = 0; int pre = -1; for (;;) { vec.push_back(now); if (pre == rin[now][0]) { pre = now; now = rin[now][1]; } else { pre = now; now = rin[now][0]; } if (now == 0) { break; } } if (vec.size() != num) { printf("-1\n"); return 0; } for (int i = 0; i < num; i++) { vector<int> d; d.push_back(vec[(i + num - 2) % num]); d.push_back(vec[(i + num - 1) % num]); d.push_back(vec[(i + num + 1) % num]); d.push_back(vec[(i + num + 2) % num]); if (sam(d, pat[vec[i]]) != 4) { printf("-1\n"); return 0; } } for (int i = 0; i < num; i++) { if (i != 0) { printf(" "); } printf("%d", vec[i] + 1); } printf("\n"); } else { vector<int> vec; for (int i = 0; i < num; i++) { vec.push_back(i); } for (;;) { bool han = true; for (int i = 0; i < num; i++) { vector<int> d; d.push_back(vec[(i + num - 2) % num]); d.push_back(vec[(i + num - 1) % num]); d.push_back(vec[(i + num + 1) % num]); d.push_back(vec[(i + num + 2) % num]); if (sam(d, pat[vec[i]]) != 4) { han = false; } } if (han) { for (int i = 0; i < num; i++) { if (i != 0) { printf(" "); } printf("%d", vec[i] + 1); } printf("\n"); return 0; } if (!next_permutation(vec.begin(), vec.end())) { break; } } printf("-1\n"); } }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> L[100005], AD[100005], node; bool vis2[100005]; int n; void DFS2(int nodo) { if (vis2[nodo]) return; vis2[nodo] = 1; node.push_back(nodo); for (int i = 0; i < AD[nodo].size(); i++) DFS2(AD[nodo][i]); } map<pair<int, int>, int> edge; bool correct1(vector<int>& v) { if (edge.find(make_pair(v[0], v[3])) != edge.end()) return 0; if (edge.find(make_pair(v[1], v[3])) != edge.end()) return 0; if (edge.find(make_pair(v[0], v[2])) != edge.end()) return 0; return 1; } bool correct2(vector<int>& v) { for (int i = 0; i < v.size(); i++) { if (edge.find(make_pair(v[i], v[(i + 1) % n])) == edge.end()) return 0; if (edge.find(make_pair(v[i], v[(i + 2) % n])) == edge.end()) return 0; if (edge.find(make_pair(v[i], v[(i - 1 + n) % n])) == edge.end()) return 0; if (edge.find(make_pair(v[i], v[(i - 2 + n) % n])) == edge.end()) return 0; } return 1; } int main() { int k; while (scanf("%d", &n) == 1) { int x, y; for (int i = 0; i < 2 * n; i++) { cin >> x >> y; x--; y--, L[x].push_back(y); L[y].push_back(x); edge[make_pair(x, y)] = edge[make_pair(y, x)] = 1; } for (int i = 0; i < n; i++) sort((L[i]).begin(), (L[i]).end()); bool yes = 1; for (int i = 0; i < n; i++) if (L[i].size() != 4) yes = 0; if (!yes) puts("-1"); else { if (n <= 6) { vector<int> v; bool ok = 0; for (int i = 0; i < n; i++) v.push_back(i); do { if (correct2(v)) { ok = 1; break; } } while (next_permutation((v).begin(), (v).end())); if (!ok) puts("-1"); else { for (int i = 0; i < n; i++) { if (i == 0) cout << v[i] + 1; else cout << " " << v[i] + 1; } puts(""); } } else { bool ok = 1; for (int i = 0; i < n; i++) { vector<int>& v = L[i]; bool entro = 0; do { if (n > 6 && correct1(v)) { AD[i].push_back(v[1]); AD[i].push_back(v[2]); entro = 1; break; } } while (next_permutation((v).begin(), (v).end())); if (!entro) { ok = 0; break; } } if (!ok) puts("-1"); else { DFS2(0); if (node.size() != n) puts("-1"); else { for (int i = 0; i < node.size(); i++) { if (i == 0) cout << node[i] + 1; else cout << " " << node[i] + 1; } } } } } } }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int A[200005], B[200005], N; int O[200005][5]; void ucitaj() { scanf("%d", &N); if (N == 5) { printf("1 5 3 4 2\n"); exit(0); } int i, j; for (i = 1; i <= 2 * N; i++) { scanf("%d%d", A + i, B + i); if (O[A[i]][1] == 0) O[A[i]][1] = B[i]; else if (O[A[i]][2] == 0) O[A[i]][2] = B[i]; else if (O[A[i]][3] == 0) O[A[i]][3] = B[i]; else O[A[i]][4] = B[i]; if (O[B[i]][1] == 0) O[B[i]][1] = A[i]; else if (O[B[i]][2] == 0) O[B[i]][2] = A[i]; else if (O[B[i]][3] == 0) O[B[i]][3] = A[i]; else O[B[i]][4] = A[i]; } } bool Sus6[10][10]; bool susedi(int a, int b) { if (Sus6[a][b]) return false; return O[a][1] == b || O[a][2] == b || O[a][3] == b || O[a][4] == b; } bool susedi6(int a, int b) { return O[a][1] == b || O[a][2] == b || O[a][3] == b || O[a][4] == b; } void sus6() { int i, j, k; for (i = 1; i <= 6; i++) { for (j = i + 1; j <= 6; j++) { for (k = i + 1; k <= 6; k++) { if (susedi6(i, j) && susedi6(j, k) && susedi6(i, k)) { Sus6[i][j] = true; Sus6[i][k] = true; Sus6[k][j] = true; Sus6[k][i] = true; Sus6[j][k] = true; Sus6[j][i] = true; } } } } } void probaj(int *A) { int i, j; if (!susedi6(A[1], A[2])) return; if (!susedi6(A[2], A[3])) return; if (!susedi6(A[3], A[4])) return; if (!susedi6(A[4], A[5])) return; if (!susedi6(A[5], A[6])) return; if (!susedi6(A[6], A[1])) return; if (!susedi6(A[1], A[3])) return; if (!susedi6(A[2], A[4])) return; if (!susedi6(A[3], A[5])) return; if (!susedi6(A[4], A[6])) return; if (!susedi6(A[5], A[1])) return; if (!susedi6(A[6], A[2])) return; if (susedi6(A[1], A[4])) return; if (susedi6(A[2], A[5])) return; if (susedi6(A[3], A[6])) return; for (i = 1; i <= 6; i++) printf("%d\n", A[i]); exit(0); } int S1[200005], S2[200005]; void ubaci() { int i, j, k, l, u, v; if (N == 6) { sus6(); for (i = 1; i <= N; i++) A[i] = i; for (i = 1; i <= 720; i++) { probaj(A); next_permutation(A + 1, A + 7); } printf("-1\n"); exit(0); } for (i = 1; i <= 2 * N; i++) { u = A[i]; v = B[i]; k = 0; if (O[u][1] == O[v][1]) k++; if (O[u][2] == O[v][1]) k++; if (O[u][3] == O[v][1]) k++; if (O[u][4] == O[v][1]) k++; if (O[u][1] == O[v][2]) k++; if (O[u][2] == O[v][2]) k++; if (O[u][3] == O[v][2]) k++; if (O[u][4] == O[v][2]) k++; if (O[u][1] == O[v][3]) k++; if (O[u][2] == O[v][3]) k++; if (O[u][3] == O[v][3]) k++; if (O[u][4] == O[v][3]) k++; if (O[u][1] == O[v][4]) k++; if (O[u][2] == O[v][4]) k++; if (O[u][3] == O[v][4]) k++; if (O[u][4] == O[v][4]) k++; if (k == 2) { if (S1[u] == 0) S1[u] = v; else S2[u] = v; if (S1[v] == 0) S1[v] = u; else S2[v] = u; } } } bool V[200005]; int Red[200005], RRR; void obidji() { int i, j, k; k = 1; j = 1; V[1] = true; Red[++RRR] = 1; while (k < N) { if (!V[S1[j]]) { j = S1[j]; V[j] = true; } else if (!V[S2[j]]) { j = S2[j]; V[j] = true; } else { printf("-1\n"); exit(0); } Red[++RRR] = j; k++; } for (i = 1; i <= N; i++) printf("%d%c", Red[i], i == N ? '\n' : ' '); } int main() { ucitaj(); ubaci(); obidji(); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const int maxN = 3 * 1000 * 100 + 50; vector<int> v[maxN], path; set<pair<int, int> > p; queue<int> q; bool mark[maxN]; int d[maxN]; int main() { ios::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < 2 * n; i++) { int a, b; cin >> a >> b; p.insert(make_pair(a, b)); v[a].push_back(b); v[b].push_back(a); } for (int i = 1; i <= n; i++) if (v[i].size() != 4) { cout << -1 << endl; return 0; } int tt, t, cnt; for (int i = 0; i < 4; i++) { for (int j = i + 1; j < 4; j++) if (p.find(make_pair(v[1][i], v[1][j])) != p.end() || p.find(make_pair(v[1][j], v[1][i])) != p.end()) { tt = v[1][i]; t = v[1][j]; memset(mark, 0, sizeof(mark)); mark[tt] = mark[t] = 1; mark[1] = 1; path.clear(); path.push_back(1); path.push_back(tt); path.push_back(t); cnt = 3; bool b = true; while (b && cnt < n) { b = false; for (int i = 0; i < 4; i++) if (mark[v[tt][i]] == 0 && (p.find(make_pair(v[tt][i], t)) != p.end() || p.find(make_pair(t, v[tt][i])) != p.end())) { int k = t; t = v[tt][i]; tt = k; mark[t] = 1; path.push_back(t); cnt++; b = true; break; } } if (cnt == n) { for (int i = 0; i < path.size(); i++) cout << path[i] << " "; cout << endl; return 0; } tt = v[1][j]; t = v[1][i]; memset(mark, 0, sizeof(mark)); mark[tt] = mark[t] = 1; mark[1] = 1; path.clear(); path.push_back(1); path.push_back(tt); path.push_back(t); cnt = 3; b = true; while (b && cnt < n) { b = false; for (int i = 0; i < 4; i++) if (mark[v[tt][i]] == 0 && (p.find(make_pair(v[tt][i], t)) != p.end() || p.find(make_pair(t, v[tt][i])) != p.end())) { int k = t; t = v[tt][i]; tt = k; mark[t] = 1; path.push_back(t); cnt++; b = true; break; } } if (cnt == n) { for (int i = 0; i < path.size(); i++) cout << path[i] << " "; cout << endl; return 0; } } } cout << -1 << endl; return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int i, j, k, x, y, z, n, m; int a[200001][10]; int adj[200001][10]; int ans[200001]; int dd[11]; bool t[11]; int be(int S, int x) { return ((x == a[S][1] || x == a[S][2] || x == a[S][3] || x == a[S][4]) ? 1 : 0); } bool dfs(int x) { if (x == 7) { for (int i = 1; i <= 6; i++) if (!be(dd[i], dd[i % 6 + 1]) || !be(dd[i], dd[(i + 1) % 6 + 1]) || !be(dd[i], dd[(i - 2 + 6) % 6 + 1]) || !be(dd[i], dd[(i - 3 + 6) % 6 + 1])) return false; return true; } for (int i = 1; i <= 6; i++) if (!t[i]) { t[i] = true; dd[x] = i; if (dfs(x + 1)) return true; dd[x] = 0; t[i] = false; } return false; } void init() { scanf("%d", &n); for (i = 1; i <= 2 * n; i++) { scanf("%d%d", &x, &y); a[x][++a[x][0]] = y; a[y][++a[y][0]] = x; } } void Main() { for (i = 1; i <= n; i++) { for (j = 1; j <= 4; j++) { x = 0; for (k = 1; k <= 4; k++) x += be(i, a[a[i][j]][k]); if (x == 2) adj[i][++adj[i][0]] = a[i][j]; } } } void print() { if (n == 5) { printf("1 2 3 4 5\n"); return; } if (n == 6) { if (dfs(1)) { for (i = 1; i < n; i++) printf("%d ", dd[i]); printf("%d\n", dd[n]); } else printf("-1\n"); return; } for (i = 1; i <= n; i++) if (adj[i][0] < 2) { printf("-1\n"); return; } int last = 1, j; ans[++ans[0]] = 1; for (i = adj[1][1]; i != 1;) { ans[++ans[0]] = i; j = i; if (last == adj[i][1]) if (last == adj[i][2]) { printf("-1\n"); return; } else i = adj[i][2]; else i = adj[i][1]; last = j; } if (ans[0] != n) { printf("-1\n"); return; } for (i = 1; i < n; i++) printf("%d ", ans[i]); printf("%d\n", ans[n]); } int main() { init(); Main(); print(); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; struct grana { int p, k; inline bool operator<(const grana &x) const { return p < x.p; } }; grana g[400005]; int poc[100005]; int sol[100005]; bool isedge(int u, int v) { for (int i = poc[u]; i < poc[u + 1]; i++) if (g[i].k == v) return 1; return 0; } int main() { int n, m; scanf("%d", &n); if (n == 5) { for (int i = 1; i <= 5; i++) printf("%d ", i); return 0; } for (int i = 1; i <= n; i++) { scanf("%d%d", &g[i].p, &g[i].k); scanf("%d%d", &g[i + n].p, &g[i + n].k); } m = n * 2; for (int i = 1; i <= m; i++) { g[i + m] = g[i]; swap(g[i + m].p, g[i + m].k); } m *= 2; sort(g + 1, g + 1 + m); for (int i = m; i >= 1; i--) poc[g[i].p] = i; poc[n + 1] = m + 1; if (n == 6) { for (int i = 1; i <= 6; i++) sol[i] = i; do { char fake = 0; for (int i = 1; i <= 6; i++) for (int j = 1; j <= 6; j++) { if (i == j) continue; if ((i + 2) % 6 + 1 == j && isedge(sol[i], sol[j])) fake = 1; } if (fake == 0) { for (int i = 1; i <= 6; i++) printf("%d ", sol[i]); return 0; } } while (next_permutation(sol + 1, sol + 7)); printf("-1"); return 0; } { int vert[4]; vert[0] = g[1].k; vert[1] = g[2].k; vert[2] = g[3].k; vert[3] = g[4].k; for (int i = 0; i < 4; i++) { int cnt = 0; for (int j = 0; j < 4; j++) cnt += isedge(vert[i], vert[j]); if (cnt == 1) sol[3] = vert[i]; } if (sol[3] == 0) { printf("-1"); return 0; } for (int i = 0; i < 4; i++) if (isedge(sol[3], vert[i])) sol[2] = vert[i]; } sol[1] = 1; for (int run = 4; run <= n; run++) { int u = sol[run - 1], v = sol[run - 2], k = sol[run - 3]; int vert[4]; vert[0] = g[poc[u]].k; vert[1] = g[poc[u] + 1].k; vert[2] = g[poc[u] + 2].k; vert[3] = g[poc[u] + 3].k; for (int i = 0; i < 4; i++) if (vert[i] != k) if (isedge(vert[i], u)) if (isedge(vert[i], v)) sol[run] = vert[i]; if (sol[run] == 0) { printf("-1"); return 0; } } if (isedge(sol[n], sol[1]) == 0) { printf("-1"); return 0; } if (isedge(sol[n], sol[2]) == 0) { printf("-1"); return 0; } if (isedge(sol[n - 1], sol[1]) == 0) { printf("-1"); return 0; } for (int i = 1; i <= n; i++) printf("%d ", sol[i]); }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class C { static StringTokenizer st; static BufferedReader in; public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = nextInt(); ArrayList<Integer>[]ages = new ArrayList[n+1]; for (int i = 1; i <= n; i++) { ages[i] = new ArrayList<Integer>(); } Set<Long> set = new HashSet<Long>(); for (int i = 1; i <= 2*n; i++) { int v1 = nextInt(); int v2 = nextInt(); long hash = (long)v1*(n+1)+v2; set.add(hash); hash = (long)v2*(n+1)+v1; set.add(hash); ages[v1].add(v2); ages[v2].add(v1); } if (n >= 7) { ArrayList<Integer>[]ages2 = new ArrayList[n+1]; for (int i = 1; i <= n; i++) { ages2[i] = new ArrayList<Integer>(); } int[]a = new int[5]; for (int i = 1; i <= n; i++) { if (ages[i].size() != 4) { System.out.println(-1); return; } for (int j = 1; j <= 4; j++) { a[j] = ages[i].get(j-1); } A: for (int j = 1; j <= 4; j++) { for (int j2 = j+1; j2 <= 4; j2++) { long hash = (long)a[j]*(n+1)+a[j2]; if (set.contains(hash)) { int k1 = 0, k2 = 0; for (int k = 1; k <= 4; k++) { if (k != j && k != j2) { hash = (long)a[j]*(n+1)+a[k]; if (set.contains(hash)) k1++; hash = (long)a[j2]*(n+1)+a[k]; if (set.contains(hash)) k2++; } } if (k1==1 && k2==1) { ages2[i].add(a[j]); ages2[i].add(a[j2]); break A; } } } } } boolean[]used = new boolean[n+1]; int v = 1; ArrayList<Integer> ans = new ArrayList<Integer>(); for (int i = 1; i <= n; i++) { if (!used[v]) ans.add(v); used[v] = true; for (int to : ages2[v]) { if (!used[to]) { v = to; break; } } } if (ans.size()==n) { for (int i : ans) { pw.print(i+" "); } pw.println(); } else pw.print(-1); } else { int[]a = new int[n+1]; for (int i = 1; i <= n; i++) { a[i] = i; } while (true) { int p = n; boolean f = true; for (int i = 1; i <= n; i++) { int next = i+1; if (next > n) next = next-n; int nex2 = i+2; if (nex2 > n) nex2 = nex2-n; long hash = (long)a[i]*(n+1)+a[next]; long hash2 = (long)a[i]*(n+1)+a[nex2]; if (!set.contains(hash) || !set.contains(hash2)) { f = false; break; } } if (f) { for (int i = 1; i <= n; i++) { System.out.print(a[i]+" "); } System.out.println(); return; } while (p > 1 && a[p] < a[p-1]) p--; if (p==1) break; int q = n; while (a[q] < a[p-1]) q--; int t = a[q]; a[q] = a[p-1]; a[p-1] = t; for (int i = 0; p+i < n-i; i++) { t = a[p+i]; a[p+i] = a[n-i]; a[n-i] = t; } } pw.print(-1); } pw.close(); } private static int nextInt() throws IOException{ return Integer.parseInt(next()); } private static long nextLong() throws IOException{ return Long.parseLong(next()); } private static double nextDouble() throws IOException{ return Double.parseDouble(next()); } private static String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } } /* 7 1 3 1 7 3 7 3 5 7 5 7 4 5 4 5 2 4 6 4 2 2 6 2 1 6 1 6 3 */
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
/** * Created with IntelliJ IDEA. * User: yuantian * Date: 3/3/13 * Time: 10:57 PM * To change this template use File | Settings | File Templates. */ import java.util.*; public class CircleOfNumbers { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[][] arcs = new int[n][5]; for(int i = 1; i <= 2*n; i++) { int from = in.nextInt()-1; int to = in.nextInt()-1; if(arcs[from][0] >= 4 || arcs[to][0] >= 4) { System.out.println(-1); return; } arcs[from][++arcs[from][0]] = to; arcs[to][++arcs[to][0]] = from; } for(int[] a :arcs) { if(a[0] != 4) { System.out.println(-1); return; } a[0] = 0; Arrays.sort(a); } if(n == 5) { System.out.println("1 2 3 4 5"); return; } int[] res = new int[n]; int[] next2 = findNext2(arcs); if(next2 == null) { System.out.println(-1); return; } int index = 0; res[++index] = next2[0]; res[++index] = next2[1]; arcs[0][0] = 1; arcs[next2[0]][0] = 1; arcs[next2[1]][0] = 1; int pre1 = next2[0], pre2 = next2[1]; while(index < n-1) { int x = findNext(arcs, pre1, pre2); if(x == -1) { System.out.println(-1); return; } res[++index] = x; arcs[x][0] = 1; pre1 = pre2; pre2 = x; } StringBuilder sb = new StringBuilder(); for(int i : res) sb.append(" ").append(i+1); System.out.println(sb.substring(1)); } static int findNext(int[][] arcs, int pre, int pre1) { int a = 1, b = 1; int count = 0; int next = -1; while(a <= 4 && b <= 4) { if(arcs[pre][a] == arcs[pre1][b]) { if(arcs[arcs[pre][a]][0] != 1) { count++; next = arcs[pre][a]; } a++; b++; } else if(arcs[pre][a] > arcs[pre1][b]) { b++; } else a++; } if(count == 1) return next; return -1; } static int[] findNext2(int[][] arcs) { int last = -1; for(int j = 1; j <= 4; j++) { int i = arcs[0][j]; int count = 0; int a = 1, b = 1; while(a <= 4 && b <= 4) { if(arcs[0][a] == arcs[i][b]) { last = arcs[0][a]; count++; a++; b++; } else if (arcs[0][a] < arcs[i][b]) a++; else b++; } if(count == 1 || (count == 2 && arcs.length == 6)) { int[] ret = new int[2]; ret[0] = last; ret[1] = i; return ret; } if(count != 2) break; } return null; } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int n; vector<vector<int> > adjlst; vector<int> res; int dir[] = {-2, -1, 1, 2}; set<int> vis; bool findRes(int adj, int i, int lim) { for (int d = 0; d < lim; d++) { int idx = (i + dir[d] + n) % n; if (res[idx] == adj) return 1; } return 0; } bool valid() { vis.clear(); vis.insert(res[0]), vis.insert(res[1]), vis.insert(res[2]), vis.insert(res[n - 1]), vis.insert(res[n - 2]); for (int i = 1; i <= n - 5; i += 1) { int num = res[i]; int mis = -1, cnt = 0; for (int j = 0; j < ((int)(adjlst[num]).size()); j++) { int adj = adjlst[num][j]; if (findRes(adj, i, 3)) continue; ++cnt; mis = adj; } if (cnt != 1) return 0; if (res[(i + 2) % n] > -1 && res[(i + 2) % n] != mis) return 0; if (vis.count(mis)) continue; res[(i + 2) % n] = mis; } for (int i = n - 4; i <= n - 1; i += 1) { int num = res[i]; for (int j = 0; j < ((int)(adjlst[num]).size()); j++) { int adj = adjlst[num][j]; if (findRes(adj, i, 4)) continue; return 0; } } return 1; } int main() { scanf("%d", &n); adjlst.resize(n + 5); int a, b; for (int i = 0; i < 2 * n; i++) { scanf("%d%d", &a, &b); adjlst[a].push_back(b); adjlst[b].push_back(a); } for (int i = 1; i <= n; i += 1) if (((int)(adjlst[i]).size()) != 4) { printf("-1"); return 0; } vector<int> one = adjlst[1]; sort((one).begin(), (one).end()); do { res = vector<int>(n, -1); res[n - 2] = one[0], res[n - 1] = one[1], res[0] = 1, res[1] = one[2], res[2] = one[3]; if (valid()) { for (int i = 0; i < n; i++) printf("%d ", res[i]); return 0; } } while (next_permutation((one).begin(), (one).end())); printf("-1"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> g[100010]; int vis[100010]; int a[200010], b[200010], s[4]; int res[100010]; int n; int f(int s[4]) { memset(vis, 0, sizeof(vis)); res[0] = s[0]; res[1] = s[1]; res[2] = 1; res[3] = s[2]; res[4] = s[3]; for (int i = 2; i <= 4; i++) vis[res[i]] = 1; int l = 4; for (int i = 3; i <= n - 1; i++) { int v = res[i], u = 1; int tt = 0; for (int j = 0; j < 4; j++) { int flag = 0; for (int k = i - 2; k <= i + 1; k++) if (g[v][j] == res[k]) flag = 1; if (!flag) tt++, u = g[v][j]; if (tt > 1) return 0; } if (tt != 1) return 0; if (!vis[u]) res[++l] = u; else return 0; } if (res[l] == res[1] && res[l - 1] == res[0]) { return 1; } return 0; } int main() { scanf("%d", &n); for (int i = 0; i < 2 * n; i++) scanf("%d%d", &a[i], &b[i]); for (int i = 0; i <= n; i++) g[i].clear(); for (int i = 0; i < 2 * n; i++) { int flag = 0; for (int j = 0; j < g[a[i]].size(); j++) if (g[a[i]][j] == b[i]) flag = 1; if (!flag) g[a[i]].push_back(b[i]); if (g[a[i]].size() > 4) { printf("-1\n"); return 0; } flag = 0; for (int j = 0; j < g[b[i]].size(); j++) if (g[b[i]][j] == a[i]) flag = 1; if (!flag) g[b[i]].push_back(a[i]); if (g[b[i]].size() > 4) { printf("-1\n"); return 0; } } for (int i = 1; i <= n; i++) if (g[i].size() != 4) { printf("-1\n"); return 0; } for (int i = 0; i < 4; i++) s[i] = g[1][i]; sort(s, s + 4); do { if (f(s)) { for (int i = 0; i < n; i++) printf("%d ", res[i]); printf("\n"); return 0; } } while (next_permutation(s, s + 4)); printf("-1\n"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double pi = acos(-1.0); const int INF = 0x7f7f7f7f; const int maxn = 111111; const int mod = 9901; template <class T> int countbit(T n) { int t = 0; while (n) n &= n - 1, ++t; return t; } template <class T> T lowbit(T n) { return n & (-n); } void File() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } vector<int> V[300000]; int ans[300000], size; bool vis[300000]; int n; bool dfs(int a, int b, int k) { if (k >= n) return true; int x, y; for (int i = 0; i < 4; ++i) { x = V[a][i]; for (int j = 0; j < 4; ++j) { y = V[b][j]; if (x == y && !vis[x]) { ans[k] = x; vis[x] = true; if (dfs(b, x, k + 1)) return true; vis[x] = false; } } } return false; } int main() { ios::sync_with_stdio(false); int a, b, i; scanf("%d", &n); for (i = 0; i < 2 * n; ++i) { scanf("%d%d", &a, &b); V[a].push_back(b); V[b].push_back(a); } try { for (i = 1; i <= n; ++i) { if (V[i].size() != 4) throw false; } ans[0] = 1; vis[1] = true; for (i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { if (i == j) continue; ans[1] = V[1][i]; ans[2] = V[1][j]; vis[ans[1]] = true; vis[ans[2]] = true; if (dfs(V[1][i], V[1][j], 3)) throw true; vis[ans[1]] = false; vis[ans[2]] = false; } } throw false; } catch (bool e) { if (!e) printf("-1"); else { for (i = 0; i < n; ++i) { if (i) putchar(' '); printf("%d", ans[i]); } } puts(""); } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; map<int, set<int> > m; for (int Q = 0; Q < 2 * n; ++Q) { int x, y; cin >> x >> y; m[x].insert(y); m[y].insert(x); } if (m.size() != n) { cout << -1; return 0; } for (int Q = 1; Q <= n; ++Q) { if (m[Q].size() != 4) { cout << -1; return 0; } } if (n == 5) { cout << 1 << " " << 2 << " " << 3 << " " << 4 << " " << 5; return 0; } set<int> used; used.insert(1); int cur = 1; vector<int> tmp(10000); vector<int> cands(10000); vector<int>::iterator r; set<int> prev; for (int Q = 1; Q <= n; ++Q) prev.insert(Q); int pp = cur; vector<int> ans; ans.push_back(cur); while (used.size() < n) { set<int> s = m[cur]; int sz = 0; for (set<int>::iterator it = s.begin(); it != s.end(); ++it) { r = set_intersection(m[*it].begin(), m[*it].end(), s.begin(), s.end(), tmp.begin()); if (r - tmp.begin() == 2) cands[sz++] = *it; } for (int Q = 0; Q < sz; ++Q) if (used.find(cands[Q]) == used.end()) if (prev.find(cands[Q]) != prev.end()) { cur = cands[Q]; break; } if (cur == pp) { cout << -1; return 0; } ans.push_back(cur); used.insert(cur); prev = s; pp = cur; } for (int Q = 0; Q < ans.size(); ++Q) cout << ans[Q] << " "; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
n = input() e = [set() for _ in range(n + 1)] def g(r): if len(r) != n: return False r = r * 3 for i in range(n): if len(e[r[i]] - set(r[i - 2 + n : i + 3 + n])): return False return True def f(): for i in range(n): if len(e[i + 1]) != 4: return [-1] if n == 5: return range(1, 6) for x in e[1]: for y in e[1]: if x != y: a, b, c = 1, x, y r = [a, b, c] for _ in range(3, n): s = (e[b] & e[c]) - set([a]) if len(s) < 1: break t = s.pop() a, b, c = b, c, t r.append(t) if g(r): return r return [-1] for _ in range(n * 2): a, b = map(int, raw_input().split()) e[a].add(b) e[b].add(a) print ' '.join(map(str, f()))
PYTHON
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int n, g[100100][5], d[100100], v[100100]; bool in[100100]; bool dfs(int a, int b, int k = 1) { v[k - 1] = a, v[k] = b; if (k < n) { in[b] = 1; for (int i = 0; i < 4; ++i) { int c = g[b][i]; if (in[c]) continue; if ((g[a][0] == c || g[a][1] == c || g[a][2] == c || g[a][3] == c) && dfs(b, c, k + 1)) return 1; } in[b] = 0; return 0; } else return 1 == b; } int main() { scanf("%d", &n); for (int i = 1, a, b; i <= 2 * n; ++i) { scanf("%d%d", &a, &b); g[a][d[a]++] = b, g[b][d[b]++] = a; if (d[a] > 4 || d[b] > 4) return 0 * puts("-1"); } for (int i = 0; i < 4; ++i) { if (dfs(1, g[1][i])) { for (int j = 0; j < n; ++j) printf("%d ", v[j]); return 0; } } return 0 * puts("-1"); }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int adj[100000][4]; int adjCounts[100000]; int N; bool was[100000]; int res[100000]; bool tryBuild() { for (int i = 3; i < N; i++) { int next = -1; for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) if (adj[res[i - 1]][j] == adj[res[i - 2]][k] && !was[adj[res[i - 2]][k]]) { next = adj[res[i - 2]][k]; was[adj[res[i - 2]][k]] = true; break; } if (next != -1) break; } if (next == -1) return false; res[i] = next; } bool f1 = false; bool f2 = false; bool f3 = false; for (int i = 0; i < 4; i++) { if (adj[res[N - 1]][i] == res[0]) f1 = true; if (adj[res[N - 1]][i] == res[1]) f2 = true; if (adj[res[N - 2]][i] == res[0]) f3 = true; } return f1 && f2 && f3; } int main() { scanf("%d", &N); memset(adjCounts, 0, sizeof(int) * 100000); for (int i = 0; i < N * 2; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; adj[a][adjCounts[a]] = b; adjCounts[a]++; adj[b][adjCounts[b]] = a; adjCounts[b]++; } bool f = false; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) if (i != j) { res[0] = 0; res[1] = adj[0][i]; res[2] = adj[0][j]; memset(was, 0, sizeof(bool) * 100000); was[0] = true; was[adj[0][i]] = true; was[adj[0][j]] = true; f = tryBuild(); if (f) break; } if (f) break; } if (!f) printf("-1\n"); else { printf("%d", res[0] + 1); for (int i = 1; i < N; i++) printf(" %d", res[i] + 1); printf("\n"); } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.io.*; import java.util.*; public class A { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); ArrayList<Integer>[] adjList = new ArrayList[n + 1]; for(int i = 1; i <= n; ++i) adjList[i] = new ArrayList<>(4); for(int i = 0; i < 2 * n; ++i) { int u = sc.nextInt(), v = sc.nextInt(); adjList[u].add(v); adjList[v].add(u); if(adjList[u].size() > 4 || adjList[v].size() > 4) { out.println(-1); out.close(); return; } } boolean good = false; boolean[] goodU = new boolean[n + 1]; for(int v: adjList[1]) goodU[v] = true; all: for(int v: adjList[1]) for(int u: adjList[v]) if(goodU[u]) { int[] ans = solve(n, adjList, v, u); if(ans != null) { good = true; for(int z: ans) out.print(z + " "); break all; } } if(!good) out.println(-1); out.close(); } static int[] solve(int n, ArrayList<Integer>[] adjList, int b, int d) { int[] ans = new int[n]; int[] used = new int[n + 1]; used[ans[0] = 1] = 2; used[ans[1] = b] = 2; used[ans[2] = d] = 2; int a = b; b = d; for(int i = 3; i < n; ++i) { for(int v: adjList[a]) if(used[v] != 2) used[v] = 1; int c = -1; for(int v: adjList[b]) if(used[v] == 1) { c = v; break; } for(int v: adjList[a]) if(used[v] != 2) used[v] = 0; if(c == -1) return null; used[ans[i] = c] = 2; a = b; b = ans[i]; } if(adjList[ans[n - 2]].contains(1) && adjList[ans[n - 1]].contains(1) && adjList[ans[n - 1]].contains(ans[1])) return ans; return null; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public Scanner(FileReader s) throws FileNotFoundException { br = new BufferedReader(s);} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException {return br.ready();} } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int n, kk, x, y, cnt[111111], to[111111][4], rr[111111]; bool w[111111]; bool go(int pp, int p, int k) { int xx, yy; for (int i = 0; i < 4; ++i) for (int j = 0; j < 4; ++j) { xx = to[pp][i], yy = to[p][j]; if (xx == yy && (!w[xx] || (k == n - 1 && xx == y))) { w[rr[k] = xx] = true; if (k == n - 1) return xx == y; return go(p, xx, k + 1); } } return false; } int main() { scanf("%d", &n); kk = n << 1; for (int i = 0; i < kk; ++i) { scanf("%d%d", &x, &y); to[x][cnt[x]++] = y; to[y][cnt[y]++] = x; } for (int i = 0; i < 4; ++i) for (int j = i + 1; j < 4; ++j) { memset(w, 0, sizeof(w)); w[1] = w[x = to[1][i]] = w[y = to[1][j]] = true; rr[0] = 1; rr[1] = x; if (go(1, x, 2)) { for (int k = 0; k < n; ++k) printf("%d ", rr[k]); return 0; } } printf("-1"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; map<pair<int, int>, bool> edge; map<pair<int, int>, bool>::iterator it; vector<vector<int> > graph; vector<int> ans; int vis[100004]; int id = 1; int main() { int n; int a, b; scanf("%d", &n); graph = vector<vector<int> >(n + 1); for (int i = 0; i < 2 * n; i++) { scanf("%d%d", &a, &b); edge[{a, b}] = 1; edge[{b, a}] = 1; graph[a].push_back(b); graph[b].push_back(a); } for (int i = 1; i <= n; i++) { if (graph[i].size() != 4) { puts("-1"); return 0; } } sort(graph[1].begin(), graph[1].end()); do { int a1 = graph[1][0], a2 = graph[1][1]; ans.clear(); ans.push_back(1); if (edge[{a1, a2}]) { ans.push_back(a1); ans.push_back(a2); } id++; vis[1] = vis[a1] = vis[a2] = id; for (int i = 4; i <= n; i++) { for (int j = 0; j < 4; j++) { int ch = graph[a2][j]; if (edge[{a1, ch}] && vis[ch] != id) { ans.push_back(ch); swap(a1, a2); a2 = ch; vis[ch] = id; break; } } } if (ans.size() == n) { for (int i = 0; i < n; i++) { printf("%d ", ans[i]); } return 0; } } while (next_permutation(graph[1].begin(), graph[1].end())); puts("-1"); }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> std::list<int> edge[100111]; std::list<int>::const_iterator it; std::vector<int> ans; int n, i, j, k, l, max; bool done[100111]; bool dfs(int now) { if (now == l) return true; for (std::list<int>::const_iterator it1(edge[ans[now]].begin()); it1 != edge[ans[now]].end(); ++it1) if (!done[*it1] && std::find(edge[ans[now + 1]].begin(), edge[ans[now + 1]].end(), *it1) != edge[ans[now + 1]].end()) { done[*it1] = true; ans[now + 2] = *it1; if (dfs(now + 1)) return true; done[*it1] = false; } return false; } int main() { scanf("%d", &n); n <<= 1; for (i = 0; i < n; ++i) { scanf("%d %d", &j, &k); edge[j].push_back(k); edge[k].push_back(j); } n >>= 1; for (i = 1; i <= n; ++i) if (edge[i].size() != 4) break; if (i <= n) puts("-1"); else { ans.reserve(n); ans.push_back(1); done[1] = true; ans.resize(n); l = n - 2; for (it = edge[1].begin(); it != edge[1].end(); ++it) if (!done[*it]) { ans[1] = *it; done[*it] = true; if (dfs(0)) break; done[*it] = false; } if (it != edge[1].end()) { for (i = 0; i < ans.size(); ++i) printf("%d ", ans[i]); puts(""); } else puts("-1"); } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> a[101011], b[101011]; bool visited[101011]; int ans[101011]; int main() { int n; cin >> n; for (int i = 0; i < n * 2; i++) { int u, v; cin >> u >> v; a[u].push_back(v); a[v].push_back(u); } bool nosolution = false; for (int i = 1; i <= n; i++) { if (a[i].size() != 4) { nosolution = true; break; } } if (nosolution == false) { ans[1] = 1; nosolution = true; for (int i2 = 0; i2 < 4; i2++) { ans[2] = a[1][i2]; for (int i3 = 0; i3 < 4; i3++) { ans[3] = a[ans[2]][i3]; if (ans[3] == ans[1]) continue; int i; memset(visited, false, sizeof visited); visited[ans[1]] = visited[ans[2]] = visited[ans[3]] = true; for (i = 4; i <= n; i++) { int what = 0; bool success = false; for (int j = 0; j < 4; j++) { what = a[ans[i - 1]][j]; if (visited[what]) continue; for (int jj = 0; jj < 4; jj++) { if (a[ans[i - 2]][jj] == what) { success = true; break; } } if (success) break; } if (success == false) break; ans[i] = what; visited[what] = true; } if (i == n + 1) { nosolution = false; break; } } if (nosolution == false) break; } } ans[n + 1] = ans[1]; ans[n + 2] = ans[2]; for (int i = 1; i <= n; i++) { b[ans[i]].push_back(ans[i + 1]); b[ans[i]].push_back(ans[i + 2]); b[ans[i + 1]].push_back(ans[i]); b[ans[i + 2]].push_back(ans[i]); } for (int i = 1; i <= n; i++) { for (int j = 0; j < 4; j++) { bool success = false; for (int k = 0; k < 4; k++) { if (b[i][k] == a[i][j]) { success = true; break; } } if (success = false) { nosolution = true; break; } } if (nosolution == false) break; } if (nosolution) cout << "-1" << endl; else { for (int i = 1; i <= n; i++) cout << ans[i] << endl; } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int vis[100100], res[100100]; vector<int> Q[100100]; int main() { int n, x, y, p, t; while (cin >> n) { for (int i = (int)0; i < (int)100100; i++) Q[i].clear(), vis[i] = 0; for (int i = (int)0; i < (int)2 * n; i++) cin >> x >> y, Q[x].push_back(y), Q[y].push_back(x); int yes = 1; for (int i = (int)1; i < (int)n + 1; i++) if (Q[i].size() != 4) yes = 0; if (yes) { for (int i = (int)0; i < (int)4; i++) res[i] = Q[1][i]; res[4] = 1; sort(res, res + 5); do { memset(vis, 0, sizeof(vis)); for (int i = (int)0; i < (int)5; i++) vis[res[i]] = 1; yes = 1; for (int i = (int)5; i < (int)n; i++) { if (!yes) break; set<int> ok, have; x = res[i - 1]; for (int j = (int)0; j < (int)4; j++) if (!vis[Q[x][j]]) ok.insert(Q[x][j]); x = res[i - 2]; for (int j = (int)0; j < (int)4; j++) if (!vis[Q[x][j]] && ok.count(Q[x][j])) have.insert(Q[x][j]); if (have.size() == 1) res[i] = *have.begin(), vis[res[i]] = 1; else yes = 0; } if (yes) { for (int i = (int)0; i < (int)n; i++) { if (!yes) break; p = res[i], x = res[(i + 1) % n], y = res[(i + 2) % n], t = 0; for (int j = (int)0; j < (int)4; j++) { if (Q[p][j] == x) t++; else if (Q[p][j] == y) t++; } if (t != 2) yes = 0; } } if (yes) break; } while (next_permutation(res, res + 5)); if (yes) { for (int i = (int)0; i < (int)n; i++) cout << res[i] << " "; puts(""); } else puts("-1"); } else puts("-1"); } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> tab[100100], ans; int n, ls[10]; bool used[100100]; bool chek(int a, int b) { for (int i = 0; i < 4; i++) if (tab[a][i] == b) return true; return false; } int main() { scanf("%d", &n); for (int i = 0; i < n + n; i++) { int a, b; scanf("%d%d", &a, &b); if (a == b) { puts("-1"); return 0; } tab[a].push_back(b); tab[b].push_back(a); } for (int i = 1; i <= n; i++) { if (tab[i].size() != 4) { puts("-1"); return 0; } } if (n <= 10) { int ar[30] = {}; for (int i = 0; i < n; i++) ar[i] = i + 1; do { ar[n] = ar[0]; ar[n + 1] = ar[1]; bool ok = true; for (int i = 0; i < n; i++) { if (!chek(ar[i], ar[i + 1]) || !chek(ar[i], ar[i + 2])) { ok = false; break; } } if (ok) { for (int i = 0; i < n; i++) printf("%d%c", ar[i], i == n - 1 ? '\n' : ' '); return 0; } } while (std::next_permutation(ar, ar + n)); puts("-1"); return 0; } int start = 1, pre = -1; while (!used[start]) { used[start] = true; ans.push_back(start); int nxt = start; for (int i = 0; i < 4; i++) { int cnt = 0; for (int j = 0; j < 4; j++) if (chek(tab[start][i], tab[start][j])) cnt++; if (cnt == 2 && tab[start][i] != pre) { nxt = tab[start][i]; break; } } if (nxt == start) { puts("-1"); return 0; } pre = start; start = nxt; } if ((int)ans.size() < n) { puts("-1"); return 0; } ans.push_back(ans[0]); ans.push_back(ans[1]); for (int i = 0; i < n; i++) { if (!chek(ans[i], ans[i + 1]) || !chek(ans[i], ans[i + 2])) { puts("-1"); return 0; } } for (int i = 0; i < n; i++) printf("%d%c", ans[i], i == n - 1 ? '\n' : ' '); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.io.*; import java.util.*; public class C { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; HashSet<Integer>[] g; int n; void checkAndOutput(int[] ans) { for (int i = 0; i < n; i++) { int v = ans[i]; int j = i + 1; if (j >= n) j -= n; if (!g[v].contains(ans[j])) { out.println(-1); return; } j++; if (j >= n) j -= n; if (!g[v].contains(ans[j])) { out.println(-1); return; } j = i - 1; if (j < 0) j += n; if (!g[v].contains(ans[j])) { out.println(-1); return; } j--; if (j < 0) j += n; if (!g[v].contains(ans[j])) { out.println(-1); return; } } for (int i = 0; i < n; i++) out.print(ans[i] + 1 + " "); out.println(); } void solve() throws IOException { n = nextInt(); g = new HashSet[n]; for (int i = 0; i < n; i++) g[i] = new HashSet<>(); for (int i = 0; i < 2 * n; i++) { int v1 = nextInt() - 1; int v2 = nextInt() - 1; g[v1].add(v2); g[v2].add(v1); } for (int i = 0; i < n; i++) if (g[i].size() != 4) { out.println(-1); return; } if (n == 5) { out.println("1 2 3 4 5"); return; } if (n == 6) { int[] ans = new int[n]; boolean[] used = new boolean[n]; int ptr = 0; outer: for (int i = 0; i < n; i++) if (!used[i]) { ans[ptr] = i; for (int j = 0; j < n; j++) if (i != j && !used[j] && !g[i].contains(j)) { ans[ptr + 3] = j; used[i] = used[j] = true; ptr++; continue outer; } out.println(-1); return; } checkAndOutput(ans); return; } int[] aux = new int[4]; int[] ans = new int[n]; cycle: for (int i = 0, v = 0, prev = -1; i < n; i++) { ans[i] = v; int ptr = 0; for (Iterator<Integer> e = g[v].iterator(); e.hasNext();) aux[ptr++] = e.next(); for (int j = 0; j < 4; j++) { int u = aux[j]; int deg = 0; for (int k = 0; k < 4; k++) if (g[u].contains(aux[k])) deg++; if (deg == 2 && u != prev) { prev = v; v = u; continue cycle; } } out.println(-1); return; } checkAndOutput(ans); } C() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new C(); } String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; template <typename T> inline void checkMax(T& a, const T& b) { if (a < b) a = b; } template <typename T> inline void checkMin(T& a, const T& b) { if (a > b) a = b; } template <typename T> struct LinkedEdge { T data; int dest; LinkedEdge<T>* next; }; template <int kMaxNode, int kMaxEdge, typename T> struct EdgeList { LinkedEdge<T>* head[kMaxNode]; LinkedEdge<T> edges[kMaxEdge]; int cnt_edge, num_node; void init(int _num_node) { num_node = _num_node; std::fill(head, head + num_node, (LinkedEdge<T>*)0); cnt_edge = 0; } LinkedEdge<T>* add_edge(int x, int y, const T& data) { edges[cnt_edge].data = data; edges[cnt_edge].dest = y; edges[cnt_edge].next = head[x]; head[x] = edges + cnt_edge; ++cnt_edge; return edges + cnt_edge - 1; } }; const int kMaxN = 100000 + 10; EdgeList<kMaxN, kMaxN * 4, int> elist; int deg[kMaxN]; int ans[kMaxN]; int n; bool IsConnect(int x, int y) { for (__typeof__(elist.head[0]) e = elist.head[x]; e != 0; e = e->next) { if (e->dest == y) return true; } return false; } void GetNeb(int start, int* arr) { int pos = 0; for (__typeof__(elist.head[0]) e = elist.head[start]; e != 0; e = e->next) arr[pos++] = e->dest; } bool Pass() { int neb[4]; bool found; for (__typeof__(3) f___LINE__ = (3), t___LINE__ = (n - 1), i = f___LINE__; i <= t___LINE__; ++i) { GetNeb(ans[i - 2], neb); found = false; for (int n___LINE__ = (4), j = 0; j < n___LINE__; ++j) { if (neb[j] == ans[i - 3] || !IsConnect(ans[i - 1], neb[j])) continue; ans[i] = neb[j]; found = true; break; } if (!found) return false; } return IsConnect(ans[0], ans[n - 1]) && IsConnect(ans[1], ans[n - 1]) && IsConnect(ans[0], ans[n - 2]); } void go() { for (int n___LINE__ = (n), i = 0; i < n___LINE__; ++i) if (deg[i] != 4) { puts("-1"); return; } int neb[4]; GetNeb(0, neb); ans[0] = 0; for (int n___LINE__ = (4), i = 0; i < n___LINE__; ++i) for (int n___LINE__ = (4), j = 0; j < n___LINE__; ++j) { if (i == j) continue; if (!IsConnect(neb[i], neb[j])) continue; ans[1] = neb[i]; ans[2] = neb[j]; if (Pass()) { for (int n___LINE__ = (n), k = 0; k < n___LINE__; ++k) printf("%d ", ans[k] + 1); puts(""); return; } } puts("-1"); } int main() { int a, b; scanf("%d", &n); elist.init(n); fill(deg, deg + n, 0); for (int n___LINE__ = (2 * n), i = 0; i < n___LINE__; ++i) { scanf("%d%d", &a, &b); --a, --b; elist.add_edge(a, b, 0); elist.add_edge(b, a, 0); deg[a]++; deg[b]++; } go(); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
/** * Created with IntelliJ IDEA. * User: Venky */ import java.io.*; import java.util.*; public class Main { static void solve() throws IOException { int n = nextInt(); int flag = 1; int[][] edges = new int[n][4]; int[] index = new int[n]; int[] ret = new int[n]; for(int i=0;i<2*n;i++) { int a = nextInt() - 1; int b = nextInt() - 1; if(index[a] < 4 && index[b] < 4) { edges[a][index[a]++] = b; edges[b][index[b]++] = a; } else flag = 0; } if(flag == 0) { out.println("-1"); return; } index = null; ret[0] = 0; all : for(int u : edges[0]) for(int v : edges[0]) { if(u == v) continue; boolean[] taken = new boolean[n]; taken[0] = true; ret[1] = u; taken[u] = true; ret[2] = v; taken[v] = true; first : for(int i=3;i<n;i++) { flag = 0; second : for(int a : edges[ret[i-1]]) for(int b : edges[ret[i-2]]) if(a == b && !taken[a]) { ret[i] = a; taken[a] = true; flag = 1; break second; } if(flag == 0) break first; } if(flag == 1) break all; } if(flag == 0) { out.println("-1"); return; } for(int i=0;i<n;i++) { if(i > 0) out.print(" "); out.print(ret[i] + 1); } } static BufferedReader br; static StringTokenizer st; static PrintWriter out; public static void main(String[] args) throws IOException { InputStream input = System.in; br = new BufferedReader(new InputStreamReader(input)); out = new PrintWriter(System.out); solve(); out.close(); } static long nextLong() throws IOException { return Long.parseLong(nextToken()); } static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } static String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = br.readLine(); if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> int ao[100000][4]; int connected(int u, int v) { int h; for (h = 0; h < 4; h++) if (ao[u][h] == v) return 1; return 0; } int qq[100000]; int solve(int n, int u, int v, int w) { int h, i, x; qq[0] = u, qq[1] = v, qq[2] = w; for (i = 3; i < n; i++) { for (h = 0; h < 4; h++) { x = ao[w][h]; if (x != u && x != v && connected(v, x)) break; } if (h == 4) return 0; qq[i] = x; u = v, v = w, w = x; } u = qq[0], v = qq[1], w = qq[n - 2], x = qq[n - 1]; return connected(u, x) && connected(v, x) && connected(u, w); } int main() { static int kk[100000]; int n, h, hu, hv, i, j, u, v; scanf("%d", &n); for (h = 0; h < n * 2; h++) { scanf("%d%d", &i, &j), i--, j--; if (kk[i] == 4 || kk[j] == 4) { printf("-1\n"); return 0; } ao[i][kk[i]++] = j; ao[j][kk[j]++] = i; } for (hu = 0; hu < 4; hu++) { u = ao[0][hu]; for (hv = hu + 1; hv < 4; hv++) { v = ao[0][hv]; if (connected(u, v) && solve(n, u, 0, v)) { for (i = 0; i < n; i++) printf("%d ", qq[i] + 1); printf("\n"); return 0; } } } printf("-1\n"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int n, x, y, cnt[100001]; bool vis[100001]; vector<vector<int> > v; vector<int> res; bool isGd(int node) { int c = 0; for (int i = (0); i < ((int)v[node].size()); i++) if (v[node][i] == res[(int)res.size() - 1] || v[node][i] == res[(int)res.size() - 2]) c++; return c == 2; } void dfs(int node) { vis[node] = 1; for (int i = (0); i < ((int)v[node].size()); i++) { if (!vis[v[node][i]] && isGd(v[node][i])) { res.push_back(v[node][i]); dfs(v[node][i]); } } } void p() { for (int i = (0); i < ((int)res.size()); i++) cout << res[i] << " "; cout << endl; } int main() { memset(cnt, 0, sizeof(cnt)); memset(vis, 0, sizeof(vis)); cin >> n; v.resize(n); for (int i = (0); i < (2 * n); i++) { cin >> x >> y; x--, y--; v[x].push_back(y); v[y].push_back(x); cnt[x]++, cnt[y]++; } for (int i = (0); i < (n); i++) if (cnt[i] != 4) { cout << -1 << endl; return 0; } res.push_back(0); for (int i = (0); i < ((int)v[0].size()); i++) { res.push_back(v[0][i]); for (int j = (0); j < ((int)v[v[0][i]].size()); j++) { if (!vis[v[v[0][i]][j]]) { vis[v[v[0][i]][j]] = vis[0] = vis[v[0][i]] = 1; res.push_back(v[v[0][i]][j]); dfs(v[v[0][i]][j]); memset(vis, 0, sizeof(vis)); if ((int)res.size() == n) goto e; res.erase(res.begin() + 2, res.end()); } } res.erase(res.begin() + 1, res.end()); } if ((int)res.size() != n) { cout << -1 << endl; return 0; } e:; cout << res[0] + 1; for (int i = (1); i < (n); i++) { cout << " " << res[i] + 1; } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.util.Arrays; import java.util.Scanner; // http://codeforces.com/contest/263/problem/C public class CircleofNumbers { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int[][] adj = new int[n + 1][4]; int[] cnt = new int[n + 1]; for (int i = 0; i < n + n; i++) { int a = s.nextInt(); int b = s.nextInt(); if (cnt[a] >= 4 || cnt[b] >= 4) { System.out.println(-1); s.close(); return; } adj[a][cnt[a]++] = b; adj[b][cnt[b]++] = a; } s.close(); for (int i = 1; i <= n; i++) { Arrays.sort(adj[i]); } boolean[] v = new boolean[n + 1]; v[1] = true; int[] ans = new int[n]; int total = 0; ans[total++] = 1; for (int i = 1, last2 = -1, last = 1; i < n; i++) { int cd = -1; int max = 0; for (int j = 0; j < 4; j++) { int b = adj[last][j]; if (!v[b]) { if (last2 != -1 && !isNeighbor(adj, last2, b)) { continue; } int match = getMatch(adj, last, b); if (cd == -1 || max < match) { cd = b; max = match; } } } if (cd != -1) { v[cd] = true; ans[total++] = cd; last2 = last; last = cd; } } if (total == n) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(ans[i] + " "); } System.out.println(sb); } else { System.out.println(-1); } } static int getMatch(int[][] adj, int a, int b) { int i = 0; int j = 0; int cnt = 0; while (i < 4 && j < 4) { if (adj[a][i] == adj[b][j]) { cnt++; i++; j++; } else if (adj[a][i] < adj[b][j]) { i++; } else { j++; } } return cnt; } static boolean isNeighbor(int[][] adj, int a, int b) { for (int j = 0; j < 4; j++) { if (adj[a][j] == b) { return true; } } return false; } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e6 + 5; const long long maxm = 500000 + 5; const long long mod = 1000000007; vector<int> e[200010], ans; map<int, bool> vis[200010]; bool mark[200010]; int n; int main() { scanf("%d", &n); for (int i = 0; i < 2 * n; i++) { int x, y; scanf("%d %d", &x, &y); e[x].push_back(y); e[y].push_back(x); vis[x][y] = true; vis[y][x] = true; } for (int i = 1; i <= n; i++) if (e[i].size() != 4) { printf("-1\n"); return 0; } int x = 1, y = 1; for (int i = 0; i < n; i++) { bool flag = false; mark[x] = true; ans.push_back(x); for (int j = 0; j < 4; j++) { int num = 0; int cur = e[x][j]; for (int k = 0; k < 4; k++) if (vis[x][e[cur][k]]) num++; if (num > 1 && !mark[cur] && vis[y][cur]) { y = x; x = cur; flag = true; break; } } if (!flag && i + 1 != n) { printf("-1\n"); return 0; } } for (int i = 0; i < n; i++) printf("%d ", ans[i]); printf("\n"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; public class CodeforcesR162Div2C { public static int[] parseNumbers(String line, int n) { int[] numbers = new int[n]; int idx = 0; for(int i = 0; i < line.length(); i++) { char c = line.charAt(i); if(Character.isDigit(c)) numbers[idx] = 10 * numbers[idx] + (c - '0'); else idx++; } return numbers; } public static void printSolution(int[] order) { StringBuilder sb = new StringBuilder(); boolean isFirst = true; for(int i : order) { if(!isFirst) sb.append(" "); sb.append((i + 1)); isFirst = false; } System.out.println(sb); } public static void main(String[] args) throws Exception { // System.setIn(new FileInputStream("CodeforcesR162Div2C.in")); BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(bf.readLine().trim()); HashSet<Integer>[] graph = new HashSet[N]; for(int i = 0; i < N; i++) graph[i] = new HashSet<Integer>(); for(int i = 0; i < 2 * N; i++) { int[] input = parseNumbers(bf.readLine().trim(), 2); int from = input[0] - 1, to = input[1] - 1; graph[from].add(to); graph[to].add(from); } for(int i = 0; i < N; i++) { if(graph[i].size() != 4) { System.out.println("-1"); return; } } if(N == 5) { System.out.println("1 2 3 4 5"); return; } boolean[] set = new boolean[N]; int[] order = new int[N]; int idx = 0; if(N == 6) { for(int i = 0; i < 3; i++) { int last; for(last = 0; last < N; last++) if(!set[last]) break; HashSet<Integer> all = new HashSet<Integer>(); for(int k = 0; k < N; k++) all.add(k); all.remove(last); for(int k : graph[last]) all.remove(k); int opp = all.iterator().next(); order[i] = last; order[i + 3] = opp; set[last] = true; set[opp] = true; } printSolution(order); return; } set[0] = true; order[idx++] = 0; while(idx != N) { int last = order[idx - 1]; for(int neighbor : graph[last]) { if(set[neighbor]) continue; int common = 0; for(int k : graph[last]) if(graph[neighbor].contains(k)) common++; if(common > 2 || common < 1) { System.out.println("-1"); return; } if(common == 1) continue; set[neighbor] = true; order[idx++] = neighbor; break; } if(last == order[idx - 1]) { System.out.println("-1"); return; } } int first = order[0]; int last = order[idx - 1]; int common = 0; for(int k : graph[last]) if(graph[first].contains(k)) common++; if(common != 2) { System.out.println("-1"); return; } printSolution(order); } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; using i64 = int64_t; using u64 = uint64_t; using pi64 = pair<i64, i64>; constexpr i64 MAXN = 3 * 100 * 1000 + 5LL; constexpr i64 MOD = 1000000007LL; constexpr i64 INF64 = MOD * MOD; bool is_possible(const vector<i64>& perm, const i64 N, const vector<vector<i64>>& adj) { if (perm.size() < N) { return false; } for (i64 i = 0; i < N; ++i) { for (const i64 j : vector<i64>{(i - 2 + N) % N, (i - 1 + N) % N, (i + 1) % N, (i + 2) % N}) { if (find(adj[perm[i]].begin(), adj[perm[i]].end(), perm[j]) == adj[perm[i]].end()) { ; return false; } } } return true; } pi64 find_nbrs(const i64 a, const vector<vector<i64>>& adj) { for (i64 j = 0; j < adj[a].size(); ++j) { const i64 b = adj[a][j]; for (i64 k = j + 1; k < adj[a].size(); ++k) { const i64 c = adj[a][k]; if (find(adj[b].begin(), adj[b].end(), c) == adj[b].end()) { continue; }; return make_pair(b, c); } } return make_pair(-1, -1); } vector<i64> find_nbrs(const pi64 p, const vector<vector<i64>>& adj) { vector<i64> isect; for (const i64 x : adj[p.first]) { if (find(adj[p.second].begin(), adj[p.second].end(), x) != adj[p.second].end()) { isect.push_back(x); } } return isect; } void try_find_erase(vector<i64>& haystack, const i64 needle) { if (const auto it = find(haystack.begin(), haystack.end(), needle); it != haystack.end()) { haystack.erase(it); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); const unsigned seed = chrono::system_clock::now().time_since_epoch().count(); mt19937 gen(seed); i64 N; cin >> N; ; vector<vector<i64>> adj(N + 1); for (i64 i = 0; i < 2 * N; ++i) { i64 A; cin >> A; ; i64 B; cin >> B; ; adj[A].push_back(B); adj[B].push_back(A); } const vector<vector<i64>> adj_const(adj); vector<i64> ans; for (i64 i = 1; i <= N; ++i) { if (adj[i].size() != 4) { goto done; } } if (N >= 7) { const pi64 nbrs1(find_nbrs(1, adj)); if (nbrs1.first < 0) { goto done; } vector<i64> nbrs1_intersect(find_nbrs(nbrs1, adj)); if (nbrs1_intersect.size() == 1) { ; ans.push_back(nbrs1.first); ans.push_back(1); ans.push_back(nbrs1.second); } else if (nbrs1_intersect.size() == 2) { const vector<i64> fst(find_nbrs(make_pair(1, nbrs1.first), adj)); const vector<i64> snd(find_nbrs(make_pair(1, nbrs1.second), adj)); ans.push_back(1); if (fst.size() == 1) { ; ans.push_back(nbrs1.second); ans.push_back(nbrs1.first); } else if (snd.size() == 1) { ; ans.push_back(nbrs1.first); ans.push_back(nbrs1.second); } else { goto done; } } else { goto done; } set<i64> seen(ans.begin(), ans.end()); for (; ans.size() < N;) { vector<i64> nbrs( find_nbrs(make_pair(*prev(prev(ans.end())), ans.back()), adj)); for (auto it = nbrs.begin(); it != nbrs.end(); ++it) { if (seen.count(*it)) { nbrs.erase(it); break; } }; if (nbrs.size() != 1) { ans.clear(); goto done; } ans.push_back(nbrs.front()); seen.insert(nbrs.front()); try_find_erase(adj[ans[ans.size() - 1]], ans[ans.size() - 3]); try_find_erase(adj[ans[ans.size() - 3]], ans[ans.size() - 1]); } } else { vector<i64> perm; for (i64 i = 1; i <= N; ++i) { perm.push_back(i); } do { if (is_possible(perm, N, adj)) { break; } } while (next_permutation(perm.begin(), perm.end())); ans = perm; } done: if (!is_possible(ans, N, adj_const)) { ans.clear(); cout << "-1\n"; } for (i64 i = 0; i < ans.size(); ++i) { cout << ans[i] << " \n"[i + 1 == N]; } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const int MAXN = 100009; int n, i, j, k, a[MAXN][5], s[MAXN], ans[MAXN], first, ct, l, now, re[10]; bool used[MAXN], flag; struct state { int num, co; } S[10]; int ex(int i, int j) { int b, c, d = 1; for (b = 0; b < 4; b++) if (a[i][b] != j) { for (c = 0; c < 4; c++) if (a[i][b] == a[j][c]) { ++d; break; } } return d; } bool fuck(state a, state b) { if (a.co != b.co) return a.co > b.co; return !used[a.num]; } bool pd() { int i, j, b[5]; for (i = 0; i < n; i++) { sort(a[re[i]], a[re[i]] + 4); b[0] = re[(i + 1) % n]; b[1] = re[(i + 2) % n]; b[2] = re[(i - 1 + n) % n]; b[3] = re[(i - 2 + n) % n]; sort(b, b + 4); for (j = 0; j < 4; j++) if (b[j] != a[re[i]][j]) return false; } return true; } void dfs(int dep) { if (dep == n) { if (pd()) flag = true; return; } if (flag) return; for (int i = 1; i <= n && !flag; i++) if (!used[i]) { used[i] = true; re[dep] = i; dfs(dep + 1); used[i] = false; } } int main() { scanf("%d", &n); memset(s, 0, sizeof(s)); memset(used, false, sizeof(used)); for (i = 1; i <= 2 * n; i++) { scanf("%d%d", &j, &k); a[j][s[j]++] = k; a[k][s[k]++] = j; if (s[j] > 4 || s[k] > 4) { printf("-1\n"); return 0; } } for (i = 1; i <= n; i++) if (s[i] != 4) { printf("-1\n"); return 0; } if (n <= 6) { flag = false; dfs(0); if (!flag) { printf("-1\n"); return 0; } for (i = 0; i < n; i++) printf("%d%c", re[i], i == n ? '\n' : ' '); return 0; } now = 1; for (i = 1; i <= n; i++) { used[now] = true; ans[i] = now; if (i == n) break; for (j = 0; j < 4; j++) { k = a[now][j]; S[j].num = k; S[j].co = ex(now, k); } sort(S, S + 4, fuck); if (used[S[0].num] || S[0].co != 3 || S[1].co != 3 || S[2].co != 2 || S[3].co != 2) { printf("-1\n"); return 0; } now = S[0].num; } for (i = 1; i <= n; i++) printf("%d%c", ans[i], i == n ? '\n' : ' '); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const int inf = (int)1e9; const int MAX_N = 200 * 1000 + 100; int n; int adj[MAX_N][4]; int len[MAX_N]; vector<int> ans; int inter(int ii, int jj) { int i = 0, j = 0; int ret = 0; while (i < 4 && j < 4) { if (adj[ii][i] == adj[jj][j]) { ret++; i++; j++; } else if (adj[ii][i] > adj[jj][j]) j++; else i++; } return ret; } int nei[4]; bool isok(vector<int> &v) { int len = ((int)v.size()); for (int i = 0; i < ((int)v.size()); i++) { nei[0] = v[(i + 1) % len]; nei[1] = v[(i + 2) % len]; nei[2] = v[(i + len - 1) % len]; nei[3] = v[(i + len - 2) % len]; sort(nei, nei + 4); for (int j = 0; j < 4; j++) { if (nei[j] != adj[v[i]][j]) return 0; } } return 1; } int f(int idx, int prev, int pprev) { vector<int> v; for (int i = 0; i < 4; i++) { if (adj[idx][i] == prev || adj[idx][i] == pprev) continue; v.push_back(adj[idx][i]); } if (inter(v[0], prev) == 1) return v[0]; return v[1]; } bool cal(int here, int prev, int pprev) { ans.clear(); ans.push_back(prev); ans.push_back(here); for (int i = 0; i < n - 2; i++) { int res = f(here, prev, pprev); if (res == -1) return 0; pprev = prev; prev = here; here = res; ans.push_back(here); } return 1; } int main() { scanf("%d", &n); for (int i = 0; i < 2 * n; i++) { int x, y; scanf("%d %d", &x, &y); x--, y--; adj[x][len[x]++] = y; adj[y][len[y]++] = x; } for (int i = 0; i < n; i++) { if (len[i] != 4) { cout << -1 << endl; return 0; } sort(adj[i], adj[i] + 4); } if (n < 10) { vector<int> perm; for (int i = 0; i < n; i++) perm.push_back(i); do { if (isok(perm)) { for (int i = 0; i < ((int)perm.size()); i++) { cout << perm[i] + 1 << endl; } return 0; } } while (next_permutation(perm.begin(), perm.end())); cout << -1 << endl; return 0; } int x = -1, y, w = -1, z; for (int i = 0; i < 4; i++) { for (int j = i + 1; j < 4; j++) { if (inter(adj[0][i], adj[0][j]) == 1) { if (x == -1) { x = adj[0][i]; y = adj[0][j]; } else { w = adj[0][i]; z = adj[0][j]; } } } } if (x == -1 || w == -1) { cout << -1 << endl; return 0; } bool res = cal(y, 0, x); if (res && isok(ans)) goto out; res = cal(z, 0, w); if (res && isok(ans)) goto out; cout << -1 << endl; return 0; out:; for (int i = 0; i < ((int)ans.size()); i++) { cout << ans[i] + 1 << " "; } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#!/usr/local/bin/python3 from __future__ import print_function import sys DEBUG = '-d' in sys.argv def debug(*args, **kwargs): if DEBUG: print(*args, file=sys.stderr, **kwargs) return None def main(): n = int(input()) cnt = [0] * (n + 1) edge = [] for i in range(0, n + 1): edge.append(set()) for i in range(0, 2 * n): s, t = map(int, input().split()) edge[s].add(t) edge[t].add(s) cnt[s] += 1 cnt[t] += 1 c4 = 0 for i in range(1, n + 1): if cnt[i] == 4: c4 += 1 if c4 != n: print(-1) else: for v2 in edge[1]: for v3 in edge[1]: if v2 in edge[v3]: mark = [True] * (n + 1) mark[1] = False mark[v2] = False res = [1, v2] i = v3 try: while True: res.append(i) mark[i] = False if len(res) == n: print(' '.join([str(x) for x in res])) sys.exit(0) for e in edge[i]: if e != i and mark[e] and res[-2] in edge[e]: i = e break if not mark[i]: raise StopIteration except StopIteration: pass print(-1) if __name__ == '__main__': main()
PYTHON3
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.io.*; import java.math.*; import static java.lang.Math.*; import static java.util.Arrays.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { new Main().run(); } StreamTokenizer in; PrintWriter out; //deb//////////////////////////////////////////////// public static void deb(String n ,Object n1){ System.out.println(n+" is : "+n1); } public static void deb(int[] A){ for (Object oo : A) { System.out.print(oo+" "); } System.out.println(""); } public static void deb(long[] A){ for (Object oo : A) { System.out.print(oo+" "); } System.out.println(""); } public static void deb(String[] A){ for (Object oo : A) { System.out.print(oo+" "); } System.out.println(""); } public static void deb(int[][] A){ for (int i = 0; i < A.length; i++) { for (Object oo : A[i]) { System.out.print(oo+" "); } System.out.println(""); } } public static void deb(long[][] A){ for (int i = 0; i < A.length; i++) { for (Object oo : A[i]) { System.out.print(oo+" "); } System.out.println(""); } } public static void deb(String[][] A){ for (int i = 0; i < A.length; i++) { for (Object oo : A[i]) { System.out.print(oo+" "); } System.out.println(""); } } ///////////////////////////////////////////////////////////// int nextInt() throws IOException { in.nextToken(); return (int)in.nval; } long nextLong() throws IOException { in.nextToken(); return (long)in.nval; } void run() throws IOException { // in = new StreamTokenizer(new BufferedReader(new FileReader("input.txt"))); // out = new PrintWriter(new FileWriter("output.txt")); in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); out.flush(); } void solve() throws IOException { int n=nextInt(); if(n==5) System.out.println("1 2 3 4 5"); else{ int[][] A= new int[n+1][4]; int[] c=new int[n+1]; int junk=0; for (int i = 0; i < 2*n; i++) { int x=nextInt(),y=nextInt(); if((c[x]==4)||(c[y]==4)){ junk=89; break; } A[x][c[x]]=y; c[x]++; A[y][c[y]]=x; c[y]++; } // deb(A[1]); // deb(A[2]); int common=0; int ll=0; int node=A[1][0]; for (int tt : A[node]) { if((tt==A[1][1])||(tt==A[1][3])||(tt==A[1][2]))common++; } if(common<2) { common=0;ll=2; node=A[1][2]; for (int tt : A[node]) { if((tt==A[1][1])||(tt==A[1][3])||(tt==A[1][0]))common++; } } if(common<2) { common=0;ll=3; node=A[1][3]; for (int tt : A[node]) { if((tt==A[1][1])||(tt==A[1][2])||(tt==A[1][0]))common++; } } if(common<2) { common=0;ll=1; node=A[1][1]; for (int tt : A[node]) { if((tt==A[1][2])||(tt==A[1][3])||(tt==A[1][0]))common++; } } if(common<2)junk=100; if(common>=2){ c[0]=-1; c[1]=1; c[2]=A[1][ll]; int c31=-1,c32=-1,cc=0; for (int j : A[c[1]]) { for (int k : A[c[2]]) { if((k==j)&&(j!=c[1])&&(j!=c[2])){ if(cc==0){ c31=k; cc++; } if(cc==1) { c32=k;break; }} } } ///// c[3]=c31; // System.out.println(c31+" "+c32); // out.print(c[1]+" "+c[2]+" "); if(c31>=0){ for (int i = 4; i < n+1; i++) { int u=0; for (int j : A[c[i-2]]) { for (int k : A[c[i-1]]) { if((k==j)&&(j!=c[i-2])&&(j!=c[i-1])&&(j!=c[i-3])){ u=1; c[i]=k; break; } } if(u==1)break; } if(u==0){junk=99;break;} } if(junk!=99){ int x=0; for (int i = 0; i < 4; i++) { if(A[1][i]==c[n])x=1; } if(x==0) junk=99; } if(junk!=99){ int x=0; for (int i = 0; i < 4; i++) { if(A[c[2]][i]==c[n])x=1; } if(x==0) junk=99; } if(junk!=99){ int x=0; for (int i = 0; i < 4; i++) { if(A[c[c[1]]][i]==c[n-1])x=1; } if(x==0) junk=99; } } if((junk==99)&&(c32>=0)){ junk=0; c[3]=c32; for (int i = 4; i < n+1; i++) { int u=0; for (int j : A[c[i-2]]) { for (int k : A[c[i-1]]) { if((k==j)&&(j!=c[i-2])&&(j!=c[i-1])&&(j!=c[i-3])){ u=1; c[i]=k; break; } } if(u==1)break; } if(u==0){junk=99;break;} } if(junk!=99){ int x=0; for (int i = 0; i < 4; i++) { if(A[c[1]][i]==c[n])x=1; } if(x==0) junk=99; } if(junk!=99){ int x=0; for (int i = 0; i < 4; i++) { if(A[c[2]][i]==c[n])x=1; } if(x==0) junk=99; } if(junk!=99){ int x=0; for (int i = 0; i < 4; i++) { if(A[c[1]][i]==c[n-1])x=1; } if(x==0) junk=99; } } } if(junk>0) out.println("-1"); else{ for (int i = 1; i < n+1; i++) { out.print(c[i]+" "); } out.println(""); } } }}
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> edges[100010], edges2[100010], out; bool marked2[100010]; int marked[100010]; bool dver[100010]; int main() { int n; cin >> n; for (int i = 0; i < 2 * n; i++) { int a, b; cin >> a >> b; edges[a].push_back(b); edges[b].push_back(a); } if (n == 5) { for (int i = 1; i <= 5; i++) out.push_back(i); } else if (n == 6) { for (int i = 0; i < 6; i++) out.push_back(-1); int p = 0; for (int i = 1; i <= 6; i++) { if (marked[i] == 0) { bool tmp = false; for (int j = 1; j <= 6; j++) if (j != i) { bool found = false; for (int k = 0; k < edges[i].size(); k++) if (edges[i][k] == j) found = true; if (!found) { out[p] = i; out[p + 3] = j; p++; tmp = true; marked[i] = 1; marked[j] = 1; } } if (!tmp) { cout << -1 << endl; return 0; } } } } else { int now = 1; out.push_back(now); while (out.size() < n) { dver[now] = true; for (int i = 0; i < edges[now].size(); i++) marked[edges[now][i]] = out.size(); int nextVertex = -1; for (int i = 0; i < edges[now].size(); i++) { int cv = edges[now][i]; if (!dver[cv]) { int tmpcntr = 0; for (int j = 0; j < edges[cv].size(); j++) if (marked[edges[cv][j]] == out.size()) tmpcntr++; if (tmpcntr == 2) { nextVertex = cv; break; } } } now = nextVertex; if (now == -1) { cout << -1 << endl; return 0; } out.push_back(now); } } for (int i = 0; i < out.size(); i++) { if (out[i] == -1) { cout << -1 << endl; return 0; } for (int k = 1; k <= 2; k++) { edges2[out[i]].push_back(out[(i + k) % out.size()]); edges2[out[(i + k) % out.size()]].push_back(out[i]); } } for (int i = 1; i <= n; i++) { if (edges[i].size() != edges2[i].size()) { cout << -1 << endl; return 0; } for (int k = 0; k < edges[i].size(); k++) marked2[edges[i][k]] = i; for (int k = 0; k < edges2[i].size(); k++) if (!marked2[edges2[i][k]]) { cout << -1 << endl; return 0; } } for (int i = 0; i < out.size(); i++) cout << out[i] << " "; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const int OO = (int)2e9; const double EPS = 1e-9; int n; int g[123456][5]; map<int, int> freq; bool vis[123456]; int sol[123456]; bool can(int a, int b, int c, int idx) { sol[idx] = b; if (idx < n) { vis[b] = 1; for (int i = 0; i < 4; i++) { int j = g[b][i]; if (vis[j] || j == c) continue; for (int k = 0; k < 4; k++) if (g[a][k] == j && can(b, j, a, idx + 1)) return 1; } vis[b] = 0; return 0; } else { if (b != 0) return 0; return 1; } } int main() { scanf("%d", &n); int a, b; bool bad = false; for (int i = 0; i < 2 * n; i++) { scanf("%d%d", &a, &b); a--, b--; freq[a]++, freq[b]++; if (freq[a] > 4 || freq[b] > 4) { bad = true; continue; } g[a][freq[a] - 1] = b; g[b][freq[b] - 1] = a; } if (bad) printf("-1"); else { for (int i = 0; i < 4; i++) if (can(0, g[0][i], 0, 1)) { for (int i = 0; i < n; i++) printf("%d ", sol[i] + 1); return 0; } printf("-1"); } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import sun.reflect.generics.tree.Tree; import java.io.*; import java.util.ArrayList; import java.util.HashSet; import java.util.Set; import java.util.TreeSet; import java.util.stream.Collectors; public class Main { static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int n = nI(); if(n == 5) { System.out.println("1 2 3 4 5"); return; } ArrayList<TreeSet<Integer>> al = new ArrayList<>(); al.add(new TreeSet<>()); for (int i = 0; i < n ; i++) { al.add(new TreeSet<>()); } for (int i = 0; i < 2*n ; i++) { int x = nI(); int y = nI(); al.get(x).add(y); al.get(y).add(x); } for(TreeSet<Integer> set : al){ if(set.size()!=4 && set.size()!=0){ System.out.println(-1); return; } } int k = 1; boolean p[] = new boolean[n+1]; p[1] = true; ArrayList<Integer> answ = new ArrayList<>(); answ.add(1); for (int i = 0; i <n-1 ; i++) { k = answ.get(answ.size()-1); for(int j : al.get(k)){ //HashSet<Integer> and = new HashSet<>(); TreeSet<Integer> and = new TreeSet<>(al.get(k)); // use the copy constructor and.retainAll(al.get(j)); //Set<Integer> and = al.get(k).stream().filter(al.get(j)::contains).collect(Collectors.toSet()); if(and.size()==2){ if(answ.size()>=2){ if(!and.contains(answ.get(answ.size()-2))){ continue; } } if(!p[j]){ p[j]=true; answ.add(j); break; } } } } if(answ.size()!=n){ System.out.println(-1); return; } for (int i = 0; i <n ; i++) { //System.out.print(answ.get(i)+" "); pw.write(answ.get(i)+" "); } pw.flush(); pw.close(); } static int nI() throws IOException { st.nextToken(); return (int)st.nval; } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.util.*; import java.io.*; public class hercules { public static int findNext(HashSet<Integer>over,ArrayList<Integer> []array,int prev,int check[],int ptr){ for(int a:array[prev]){ if(over.contains(a))continue; int cnt=0; for(int b:array[a]){ if(check[ptr-1]==b) cnt++; } //System.out.println("1"+" "+prev+" "+cnt+" "+a); if(cnt==1) return a; } return 0; } public static void main(String [] args)throws Exception{ BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(bf.readLine()); ArrayList<Integer> [] array=new ArrayList[n + 1]; int check[]=new int[n+1]; for(int i=0;i<=n;i++) array[i]=new ArrayList(); for(int i=0;i<2*n;i++){ StringTokenizer st=new StringTokenizer(bf.readLine()); int a=Integer.parseInt(st.nextToken()); int b=Integer.parseInt(st.nextToken()); array[a].add(b); array[b].add(a); check[a]++;check[b]++; } //for(int i=1;i<=n;i++)System.out.print(check[i]+" "); for(int i=1;i<=n;i++) if(check[i]!=4){ System.out.print("-1"); return; } if(n==5){ System.out.print("1 2 3 4 5"); return; } HashSet<Integer>over=new HashSet<Integer>(); HashSet<Integer>hs=new HashSet<Integer>(); StringBuilder sb=new StringBuilder(); Arrays.fill(check,0); int ptr=1; check[1]=1; int prev=1; over.add(1); for(int a:array[prev]) hs.add(a); for(int a:array[prev]){ if(over.contains(a))continue; int cnt=0; for(int b:array[a]){ if(hs.contains(b)) cnt++; } if(cnt==2){ ptr++; hs=new HashSet<Integer>(); check[ptr]=a; over.add(a); prev=a; for(int b:array[prev]) hs.add(b); break; } } if(n==6){ int sum=21; for(int a:array[1])sum-=a; check[4]=sum-1; over.add(sum-1); //System.out.println(check[3]); sum=21-check[2]; for(int a:array[check[2]])sum-=a; check[5]=sum; over.add(sum); //System.out.println(over); ptr++; for(int i=1;i<=6;i++) if(!over.contains(i)){ //System.out.println(ptr+" "+i); check[ptr]=i;ptr+=3; } for(int i=1;i<=6;i++) sb.append(check[i]+" "); System.out.print(sb); return; } //System.out.println(hs+" "+prev); for(int a:array[prev]){ if(over.contains(a))continue; int cnt=0; for(int b:array[a]){ if(hs.contains(b)) cnt++; } //System.out.println("3"+" "+cnt+" "+a); if(cnt==2){ ptr++; check[ptr]=a; over.add(a); prev=a; break; } } //System.out.println(over+" "+prev); //System.out.println(a+" "+cnt+" "+prev+" "+ptr); while(over.size()<n){ int a=findNext(over,array,prev,check,ptr); ptr++; //System.out.println(ptr+" "+a); if(ptr>n){ System.out.print("-1"); return; } check[ptr]=a; prev=a; over.add(a); } //System.out.println(over+" "+ptr); for(int i=1;i<=n;i++) sb.append(check[i]+" "); System.out.print(sb); } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> a[100020]; int n, m, x, y; int v[100020]; int w[100020]; int s[100020]; int ooo = 1e9; int ok(int x) { ooo++; for (int i : a[s[x]]) v[i] = ooo; if (v[s[(x + 1) % n]] != ooo) return 0; if (v[s[(x + 2) % n]] != ooo) return 0; if (v[s[(x - 1 + n) % n]] != ooo) return 0; if (v[s[(x - 2 + n) % n]] != ooo) return 0; } int main() { cin >> n; for (int i = 0; i < 2 * n; i++) { scanf("%d%d", &x, &y); a[x].push_back(y); a[y].push_back(x); } for (int i = 1; i <= n; i++) { if (a[i].size() != 4) { puts("-1"); return 0; } } sort(a[1].begin(), a[1].end()); int done = 0; do { memset(v, 0, sizeof v); memset(w, 0, sizeof w); w[a[1][0]] = 1; w[a[1][1]] = 1; w[a[1][2]] = 1; w[a[1][3]] = 1; w[1] = 1; done = 1; s[n - 1] = a[1][2]; s[n] = s[0] = a[1][3]; s[1] = 1; s[2] = a[1][0]; s[3] = a[1][1]; if (s[n - 1] == 2 && s[n] == 6) s[n] = 6; for (int i = 2; i + 2 < n - 1; i++) { v[s[i - 2]] = i; v[s[i - 1]] = i; v[s[i + 1]] = i; for (int j : a[s[i]]) if (v[j] != i && !w[j]) { s[i + 2] = j; w[j] = 1; goto can; } done = 0; break; can:; } if (s[n - 1] == 2 && s[n] == 6) s[n] = 6; if (done && ok(2) && ok(3) && ok(n - 1) && ok(n)) { for (int i = 1; i <= n; i++) printf("%d ", s[i]); puts(""); return 0; } } while (next_permutation(a[1].begin(), a[1].end())); puts("-1"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int a[100002][5]; int f[100002], ans[100002], v[100002]; int n, ff; void dfs(int l) { if (ff == 1) return; int s = 0; int i; if (l > n) { s = 0; for (i = 0; i < f[ans[n]]; i++) { if (a[ans[n]][i] == 1) { s++; } if (a[ans[n]][i] == ans[2]) { s++; } } if (s != 2) return; ff = 1; return; } int j; for (i = 0; i < f[ans[l - 1]]; i++) { if (ff == 1) return; int t = a[ans[l - 1]][i]; if (v[t] == 1) continue; s = 0; for (j = 0; j < f[t]; j++) { if (a[t][j] == ans[l - 2]) { s = 1; break; } } if (s == 0) continue; ans[l] = a[ans[l - 1]][i]; v[t] = 1; dfs(l + 1); v[t] = 0; } } int main() { while (scanf("%d", &n) != EOF) { memset(f, 0, sizeof(f)); int i, x, y; int flag = 0; for (i = 0; i < 2 * n; i++) { scanf("%d%d", &x, &y); f[x]++; if (f[x] > 4) { flag = 1; } f[y]++; if (f[y] > 4) { flag = 1; } if (flag == 0) { a[x][f[x] - 1] = y; a[y][f[y] - 1] = x; } } if (flag == 1) printf("-1\n"); else { ff = 0; ans[1] = 1; for (i = 0; i < f[1]; i++) { memset(v, 0, sizeof(v)); v[1] = 1; v[a[1][i]] = 1; ans[2] = a[1][i]; dfs(3); if (ff == 1) break; } if (ff == 0) { printf("-1\n"); continue; } for (i = 1; i <= n; i++) { if (i > 1) printf(" "); printf("%d", ans[i]); } printf("\n"); } } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; public class C{ // Scanner sc=new Scanner(System.in); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int INF=1<<28; double EPS=1e-9; int n; int[] a, b; void run(){ try{ n=Integer.parseInt(br.readLine()); a=new int[2*n]; b=new int[2*n]; for(int i=0; i<2*n; i++){ String[] ss=br.readLine().split(" "); a[i]=Integer.parseInt(ss[0])-1; b[i]=Integer.parseInt(ss[1])-1; } solve(); }catch(Exception e){} } void solve(){ HashSet<Integer>[] sets=new HashSet[n]; ArrayList<Integer>[] es=new ArrayList[n]; for(int i=0; i<n; i++){ es[i]=new ArrayList<Integer>(); sets[i]=new HashSet<Integer>(); } for(int i=0; i<2*n; i++){ es[a[i]].add(b[i]); es[b[i]].add(a[i]); sets[a[i]].add(b[i]); sets[b[i]].add(a[i]); } for(int i=0;i<n;i++){ if(es[i].size()!=4){ println("-1"); return; } } int[] vs=new int[n]; int[] check=new int[n]; int c=1; for(int second : es[0]){ for(int third : es[0]){ if(second==third){ continue; } vs[0]=0; vs[1]=second; vs[2]=third; check[vs[0]]=check[vs[1]]=check[vs[2]]=c; boolean ok=true; for(int i=3; i<n; i++){ // debug(i); vs[i]=-1; for(int v : es[vs[i-1]]){ if(check[v]<c&&sets[v].contains(vs[i-2])){ vs[i]=v; check[vs[i]]=c; break; } } // debug(ok); if(vs[i]==-1){ ok=false; break; } // debug(vs); } // debug(); if(ok){ StringBuilder sb=new StringBuilder(); for(int i=0; i<n; i++){ sb.append(vs[i]+1); if(i<n-1){ sb.append(' '); } } println(sb.toString()); return; } c++; } } /* * * HashSet<Integer>[] sets=new HashSet[n]; for(int i=0; i<n; i++){ * sets[i]=new HashSet<Integer>(); } HashMap<Long, Integer> map=new * HashMap<Long, Integer>(); TreeSet<Integer> OKE=new * TreeSet<Integer>(); for(int i=0; i<2*n; i++){ sets[a[i]].add(b[i]); * sets[b[i]].add(a[i]); long e1=a[i]*(long)n+b[i]; long * e2=b[i]*(long)n+a[i]; map.put(e1, i); map.put(e2, i); OKE.add(i); } * for(int i=0; i<n; i++){ if(sets[i].size()!=4){ println("-1"); return; * } } int[] vs=new int[n]; boolean[] used=new boolean[n]; for(;;){ * if(OKE.isEmpty()){ break; } int first=OKE.first(); vs[0]=a[first]; * vs[1]=b[first]; OKE.remove(first); fill(used, false); * used[vs[0]]=used[vs[1]]=true; boolean good=true; for(int i=2; i<n; * i++){ // debug(i); boolean ok=false; for(int v : sets[vs[i-1]]){ * if(!used[v]&&sets[v].contains(vs[i-2])){ vs[i]=v; used[vs[i]]=true; * long e=vs[i-1]*(long)n+vs[i]; OKE.remove(map.get(e)); ok=true; break; * } } // debug(ok); if(!ok){ good=false; break; } } if(good){ * StringBuilder sb=new StringBuilder(); for(int i=0; i<n; i++){ * sb.append(vs[i]+1); if(i<n-1){ sb.append(' '); } } * println(sb.toString()); return; } } */ println("-1"); } void println(String s){ System.out.println(s); } void print(String s){ System.out.print(s); } void debug(Object... os){ System.err.println(Arrays.deepToString(os)); } public static void main(String[] args){ Locale.setDefault(Locale.US); new C().run(); } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Random; import java.util.StringTokenizer; public class Solution{ static int n; static HashSet<Integer>[] g; static ArrayList<Integer> list; static boolean[] visited; public static void main(String[] args) throws IOException { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int tt = 1; while(tt-->0) { n = fs.nextInt(); g = new HashSet[n]; for(int i=0;i<n;i++) { g[i] = new HashSet<Integer>(); } for(int i=0;i<2*n;i++) { int a = fs.nextInt()-1, b = fs.nextInt()-1; g[a].add(b); g[b].add(a); } for(int i=0;i<n;i++) { if(g[i].size()!=4) { out.println("-1"); out.flush(); return; } } HashSet[] G = g; ArrayList<Integer> el = new ArrayList<Integer>(g[0]); list = new ArrayList<Integer>(); visited = new boolean[n]; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(i==j) continue; if(!g[el.get(i)].contains(el.get(j))) continue; if(check(0, el.get(i), el.get(j))) { for(int k: list) { out.print(k+1+" "); } out.flush(); return; } } } out.println(-1); } out.close(); } static boolean check(int a,int b,int c) { list.clear(); Arrays.fill(visited, false); visited[a] = true; visited[b] = true; visited[c] = true; list.add(a); list.add(b); list.add(c); int k = n-3; outer: while(k>0) { for(int i: g[b]) { if(g[c].contains(i) && !visited[i]) { list.add(i); visited[i] = true; k--; b =c; c = i; continue outer; } } return false; } return true; } static final Random random=new Random(); static <T> void shuffle(T[] arr) { int n = arr.length; for(int i=0;i<n;i++ ) { int k = random.nextInt(n); T temp = arr[k]; arr[k] = arr[i]; arr[i] = temp; } } static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); int temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void reverse(int[] arr, int l, int r) { for(int i=l;i<l+(r-l)/2;i++){ int temp = arr[i]; arr[i] = arr[r-i+l-1]; arr[r-i+l-1] = temp; } } static void reverse(long[] arr, int l, int r) { for(int i=l;i<l+(r-l)/2;i++){ long temp = arr[i]; arr[i] = arr[r-i+l-1]; arr[r-i+l-1] = temp; } } static <T> void reverse(T[] arr, int l, int r) { for(int i=l;i<l+(r-l)/2;i++) { T temp = arr[i]; arr[i] = arr[r-i+l-1]; arr[r-i+l-1] = temp; } } static class FastScanner{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next(){ while(!st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt(){ return Integer.parseInt(next()); } public int[] readArray(int n){ int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } public long nextLong() { return Long.parseLong(next()); } public char nextChar() { return next().toCharArray()[0]; } } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> vec[100005]; vector<int> ans; int visited[100005]; int main() { long long n, x, y, flag = 0, prev = 1, tempx, tempy, flag2 = 0; cin >> n; for (int i = 0; i < 2 * n; i++) { cin >> x >> y; vec[x].push_back(y); vec[y].push_back(x); } for (int i = 1; i <= n; i++) { if (vec[i].size() != 4) { cout << "-1"; return 0; } } if (n == 5) { cout << "1 2 3 4 5"; return 0; } for (int i = 0; i < vec[1].size(); i++) { for (int pp = i + 1; pp < vec[1].size(); pp++) { x = vec[1][i]; y = vec[1][pp]; flag = 0; flag2 = 0; for (int j = 0; j < vec[x].size(); j++) { if (vec[x][j] == y) { flag = 1; break; } } if (flag == 0) continue; tempx = 0, tempy = 0; for (int j = 0; j < vec[x].size(); j++) { for (int p = 0; p < vec[1].size(); p++) if (vec[x][j] == vec[1][p]) { tempx++; } } for (int j = 0; j < vec[y].size(); j++) { for (int p = 0; p < vec[1].size(); p++) if (vec[y][j] == vec[1][p]) { tempy++; } } if (tempx == 2 && tempy == 2 && n == 6) { ans.push_back(1); ans.push_back(y); ans.push_back(x); flag2 = 1; break; } if (tempx == 1 && tempy == 2) { ans.push_back(1); ans.push_back(y); ans.push_back(x); flag2 = 1; break; } if (tempx == 2 && tempy == 1) { ans.push_back(1); ans.push_back(x); ans.push_back(y); flag2 = 1; break; } } if (flag2) break; } visited[1] = 1; visited[x] = 1; visited[y] = 1; int t = ans.size(); if (t != 3) { cout << "-1" << endl; return 0; } while (t != n) { x = ans[t - 2]; y = ans[t - 1]; flag = 0; for (int i = 0; i < vec[x].size(); i++) { for (int j = 0; j < vec[y].size(); j++) { if (vec[x][i] == vec[y][j] && !visited[vec[x][i]]) { ans.push_back(vec[x][i]); visited[vec[x][i]] = 1; flag = 1; break; } } if (flag == 1) break; } if (flag == 0) { cout << "-1"; return 0; } t = ans.size(); } for (int i = 0; i < ans.size(); i++) cout << ans[i] << " "; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.util.*; import java.io.*; import java.math.BigInteger; import static java.lang.Math.*; public class Main { int [] way; boolean check (int n, int [][] a){ boolean used[] = new boolean[n]; used[0] = used[way[1]] = used[way[2]] = true; for (int i = 3; i < n; i++){ boolean flag = false; int w1 = way[i-1]; int w2 = way[i-2]; for (int j = 0; j < 4; j++){ if (used[a[w1][j]]) continue; boolean ok = false; for (int k = 0; k < 4; k++) if (a[w2][k] == a[w1][j]) ok = true; if (!ok) continue; flag = true; used[a[w1][j]] = true; way[i] = a[w1][j]; break; } if (!flag) return false; } for (int i = 0; i<n; i++){ for (int j = 1; j <= 2; j++){ int temp = (i + j) % n; boolean ok = false; for (int k = 0; k < 4; k++) if (a[way[i]][k] == way[temp]) ok = true; if (!ok) return false; } } return true; } void print(int [] way){ for (int i = 0; i<way.length; i++) out.print((way[i] + 1) + " "); out.println(); } void solve() throws Exception { int n = in.nextInt(); int [] digits = new int[n]; ArrayList<Integer> [] l = new ArrayList[n]; for (int i = 0; i<n; i++) l[i] = new ArrayList<Integer>(); for (int i = 0; i < 2 * n; i++){ int u = in.nextInt() - 1; int v = in.nextInt() - 1; l[u].add(v); l[v].add(u); digits[v]++; digits[u]++; } for (int i = 0; i < n; i++){ if (digits[i] != 4){ out.println(-1); return; } } int [][] a = new int[n][5]; for (int i = 0; i<n; i++){ for(int j = 0; j<l[i].size(); j++) a[i][j] = l[i].get(j); } way = new int[n]; for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++){ if (i == j) continue; Arrays.fill(way, 0); way[0] = 0; way[1] = a[0][i]; way[2] = a[0][j]; if (check (n, a)){ print(way); return; } //print(way); } out.println(-1); } FastScanner in; PrintWriter out; String input = ""; String output = ""; void run() { try { if (input.length() > 0) { in = new FastScanner(new BufferedReader(new FileReader(input))); } else in = new FastScanner(new BufferedReader(new InputStreamReader( System.in))); if (output.length() > 0) out = new PrintWriter(new FileWriter(output)); else out = new PrintWriter(System.out); solve(); out.flush(); out.close(); } catch (Exception ex) { ex.printStackTrace(); out.flush(); out.close(); } finally { out.close(); } } public static void main(String[] args) { new Main().run(); } class FastScanner { BufferedReader bf; StringTokenizer st; public FastScanner(BufferedReader bf) { this.bf = bf; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(bf.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public String nextLine() throws IOException { return bf.readLine(); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] readIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = this.nextInt(); return array; } public long[] readLongArray(int n) throws IOException { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = this.nextLong(); return array; } } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int n; vector<vector<int> > adj(1e5 + 1); set<int> rem; vector<int> ord; void search(int pred1, int pred2) { if (ord.size() == n) return; int nxt = 0; for (int j = 0; j < 4 && nxt == 0; j++) { for (int k = 0; k < 4; k++) { int x = adj[pred1][j], y = adj[pred2][k]; if (x == y && rem.find(x) != rem.end()) { nxt = x; rem.erase(x); break; } } } if (nxt) { ord.push_back(nxt); search(nxt, pred1); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < 2 * n; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = 1; i <= n; i++) { if (adj[i].size() != 4) { cout << -1 << '\n'; return 0; } } for (int i = 0; i < 4; i++) { for (int j = i + 1; j < 4; j++) { rem.clear(); ord.clear(); for (int k = 2; k <= n; k++) { rem.insert(k); } ord.push_back(adj[1][i]); rem.erase(adj[1][i]); ord.push_back(1); ord.push_back(adj[1][j]); rem.erase(adj[1][j]); search(adj[1][j], 1); if (ord.size() == n) { for (int i = 0; i < n; i++) { cout << ord[i] << ' '; } cout << '\n'; return 0; } } } cout << -1 << '\n'; return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int n, a, b; vector<int> adj[100010]; map<pair<int, int>, int> m; vector<int> res; bool visited[100010]; bool tenta(int a, int b, int c, int size) { visited[a] = visited[b] = visited[c] = true; if (m[pair<int, int>(a, b)] and m[pair<int, int>(b, c)] and m[pair<int, int>(a, c)]) { for (int i = 0; i < (int)adj[c].size(); i++) { int u = adj[c][i]; if (u == 0) { if (m[pair<int, int>(0, b)] and m[pair<int, int>(c, res[1])] and size == n) return true; continue; } if (visited[u]) continue; if (u != b and m[pair<int, int>(u, b)]) { res.push_back(u); if (tenta(b, c, u, size + 1)) return true; res.pop_back(); } } } return false; } bool brute() { for (int i = 0; i < (int)adj[0].size(); i++) { int u = adj[0][i]; for (int j = 0; j < (int)adj[u].size(); j++) { int v = adj[u][j]; if (v == 0) continue; res.push_back(u); res.push_back(v); if (m[pair<int, int>(v, 0)]) { memset(visited, false, n * sizeof(bool)); if (tenta(0, u, v, 3)) return true; } res.pop_back(); res.pop_back(); } } return false; } int main() { scanf("%d", &n); for (int i = 0; i < 2 * n; i++) { scanf("%d %d", &a, &b); a--, b--; adj[a].push_back(b); adj[b].push_back(a); m[pair<int, int>(a, b)] = m[pair<int, int>(b, a)] = 1; } for (int i = 0; i < n; i++) if (adj[i].size() != 4) { printf("-1\n"); return 0; } res.push_back(0); if (!brute()) printf("-1\n"); else { for (int i = 0; i < (int)res.size(); i++) printf("%d ", res[i] + 1); printf("\n"); } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 7; vector<long long> g[N]; long long ans[N]; bool used[N]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n; cin >> n; for (long long i = 0; i < 2 * n; ++i) { long long u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (long long i = 1; i <= n; ++i) { if (g[i].size() != 4) { cout << "-1\n"; exit(0); } } ans[0] = 1; for (long long x : g[1]) { for (long long y : g[1]) { if (x != y) { ans[1] = x; ans[2] = y; memset(used, 0, sizeof used); used[1] = used[x] = used[y] = 1; bool ban = 0; for (long long i = 3; i < n; ++i) { set<long long> ms; for (long long e : g[x]) ms.insert(e); vector<long long> can; for (long long e : g[y]) { if (ms.find(e) != ms.end()) { can.push_back(e); } } bool first = 0; for (long long e : can) { if (!used[e]) { x = y; y = e; used[e] = 1; ans[i] = e; first = 1; break; } } if (!first) { ban = 1; break; } } if (!ban) { for (long long i = 0; i < n; ++i) cout << ans[i] << ' '; cout << '\n'; exit(0); } } } } cout << "-1\n"; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000; int n; vector<int> graf[MAXN + 3]; bool odw[MAXN + 3]; int st[MAXN + 3], color[MAXN + 3]; int wyn[MAXN + 3], ind; void wczytaj() { scanf("%d", &n); for (int i = 0; i < 2 * n; i++) { int a, b; scanf("%d %d", &a, &b); graf[a].push_back(b); graf[b].push_back(a); st[a]++; st[b]++; } } bool check_st() { for (int i = 1; i <= n; i++) if (st[i] != 4) return false; return true; } inline bool check_neighbours(int v, int u) { int licznik = 0; for (int i = 0; i < 4; i++) if (color[graf[u][i]] == v) licznik++; if (licznik == 2) return true; return false; } void dfs(int v) { odw[v] = true; wyn[ind++] = v; for (int i = 0; i < 4; i++) color[graf[v][i]] = v; for (int i = 0; i < 4; i++) { int u = graf[v][i]; if (!odw[u] && check_neighbours(v, u)) dfs(u); } } inline bool check_connect(int v, int x, int y) { bool spr = false; for (int i = 0; i < 4; i++) if (graf[v][i] == x) { spr = true; break; } if (!spr) return false; spr = false; for (int i = 0; i < 4; i++) if (graf[v][i] == y) { spr = true; break; } return spr; } inline bool check_connect(int v, int x) { bool spr = false; for (int i = 0; i < 4; i++) if (graf[v][i] == x) { spr = true; break; } return spr; } void brut() { int t[10]; for (int i = 1; i <= n; i++) t[i] = i; do { bool spr = true; spr = check_connect(t[1], t[n]); for (int i = 2; i <= n && spr; i++) spr = check_connect(t[i], t[i - 1]); for (int i = 1; i <= n && spr; i++) { int p = (i - 2 >= 1) ? i - 2 : i + n - 2; int k = (i + 2 <= n) ? i + 2 : i - n + 2; spr = check_connect(t[i], t[p], t[k]); } if (spr) { for (int i = 1; i <= n; i++) printf("%d ", t[i]); puts(""); return; } } while (next_permutation(t + 1, t + n + 1)); } int main() { wczytaj(); if (!check_st()) { puts("-1"); return 0; } if (n > 6) { dfs(1); if (ind != n) puts("-1"); else { for (int i = 0; i < n; i++) printf("%d ", wyn[i]); puts(""); } } else brut(); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:65777216") using namespace std; const long double EPS = 1e-11; const int INF = 2000000000; const long long lINF = 9223372036854775807; const long double PI = 3.14159265358979323846; vector<long long> ans; void cans() { for (int i = 0; i < ans.size(); i++) cout << ans[i] + 1 << ' '; } int main() { int n, i, j, x, y, v, to, t, c1 = -1, c2 = -1, c3 = -1, c4 = -1, c5 = -1, k1, k2, k; int u[100005]; vector<long long> a[100005]; cin >> n; for (i = 0; i < n * 2; i++) { cin >> x >> y; x--; y--; a[x].push_back(y); a[y].push_back(x); } if (n == 5) { cout << 1 << ' ' << 2 << ' ' << 3 << ' ' << 4 << ' ' << 5; return 0; } for (i = 0; i < n; i++) if (a[i].size() != 4) { cout << -1; return 0; } c3 = 0; for (i = 0; i < n; i++) u[i] = 0; u[0] = 1; u[a[0][0]] = 1; u[a[0][1]] = 1; u[a[0][2]] = 1; k = 0; for (i = 0; i < 4; i++) if (u[a[a[0][3]][i]]) k++; if (k == 2) { } else if (k == 3) { c4 = a[0][3]; for (i = 0; i < 4; i++) if (u[a[a[0][3]][i]]) u[a[a[0][3]][i]]--; for (i = 0; i < n; i++) if (u[i] == 1) { c1 = i; break; } } else { cout << -1; return 0; } for (i = 0; i < n; i++) u[i] = 0; u[0] = 1; u[a[0][0]] = 1; u[a[0][1]] = 1; u[a[0][3]] = 1; k = 0; for (i = 0; i < 4; i++) if (u[a[a[0][2]][i]]) k++; if (k == 2) { } else if (k == 3) { if (c4 == -1) c4 = a[0][2]; else c2 = a[0][2]; for (i = 0; i < 4; i++) if (u[a[a[0][2]][i]]) u[a[a[0][2]][i]]--; for (i = 0; i < n; i++) if (u[i] == 1) { if (c1 == -1) c1 = i; else c5 = i; break; } } else { cout << -1; return 0; } for (i = 0; i < n; i++) u[i] = 0; u[0] = 1; u[a[0][0]] = 1; u[a[0][2]] = 1; u[a[0][3]] = 1; k = 0; for (i = 0; i < 4; i++) if (u[a[a[0][1]][i]]) k++; if (k == 2) { } else if (k == 3) { if (c4 == -1) c4 = a[0][1]; else c2 = a[0][1]; for (i = 0; i < 4; i++) if (u[a[a[0][1]][i]]) u[a[a[0][1]][i]]--; for (i = 0; i < n; i++) if (u[i] == 1) { if (c1 == -1) c1 = i; else c5 = i; break; } } else { cout << -1; return 0; } for (i = 0; i < n; i++) u[i] = 0; u[0] = 1; u[a[0][1]] = 1; u[a[0][2]] = 1; u[a[0][3]] = 1; k = 0; for (i = 0; i < 4; i++) if (u[a[a[0][0]][i]]) k++; if (k == 2) { } else if (k == 3) { if (c4 == -1) c4 = a[0][0]; else c2 = a[0][0]; for (i = 0; i < 4; i++) if (u[a[a[0][0]][i]]) u[a[a[0][0]][i]]--; for (i = 0; i < n; i++) if (u[i] == 1) { if (c1 == -1) c1 = i; else c5 = i; break; } } else { cout << -1; return 0; } ans.push_back(c1); ans.push_back(c2); ans.push_back(c3); ans.push_back(c4); ans.push_back(c5); u[c1] = 1; u[c2] = 1; u[c3] = 1; u[c4] = 1; u[c5] = 1; while (true) { j = ans.size() - 2; for (i = 0; i < 4; i++) { to = a[ans[j]][i]; if (!u[to]) { ans.push_back(to); u[to] = 1; break; } } if (i == 4) { if (ans.size() == n) { for (i = 0; i < 4; i++) if (a[ans[j]][i] != ans[j - 1] && a[ans[j]][i] != ans[j - 2] && a[ans[j]][i] != ans[j + 1] && a[ans[j]][i] != ans[0]) { cout << -1; return 0; } for (i = 0; i < 4; i++) if (a[ans[j + 1]][i] != ans[j] && a[ans[j + 1]][i] != ans[j - 1] && a[ans[j + 1]][i] != ans[0] && a[ans[j + 1]][i] != ans[1]) { cout << -1; return 0; } cans(); return 0; } else { cout << -1; return 0; } } } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const int N = 100010; vector<int> v[N]; int num[N]; bool vis[N]; int len, n; bool Doit() { int i; for (i = 1; i <= n; i++) { if (v[i].size() != 4) return 0; } return 1; } bool DFS(int a, int fa, int th) { if (th == n + 1) return 1; if (!vis[a]) { int j; bool f1, f2; vis[a] = 1; num[th] = a; vector<int>::iterator it, it1; for (it = v[a].begin(); it != v[a].end(); it++) { if (fa == -1) { if (DFS((*it), a, th + 1)) return 1; } else { f1 = f2 = 0; j = (*it); if (j == fa) continue; for (it1 = v[j].begin(); it1 != v[j].end(); it1++) { if ((*it1) == a) f1 = 1; if ((*it1) == fa) f2 = 1; } if (f1 && f2) { if (DFS(j, a, th + 1)) return 1; } } } vis[a] = 0; } return 0; } int main() { int i, a, b; while (scanf("%d", &n) != EOF) { for (i = 1; i <= n; i++) { v[i].clear(); } for (i = 1; i <= 2 * n; i++) { scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); } if (Doit()) { memset(vis, 0, sizeof(vis)); len = 0; if (DFS(1, -1, 1)) { for (i = 1; i <= n; i++) { if (i != 1) printf(" "); printf("%d", num[i]); } printf("\n"); } else printf("-1\n"); } else { printf("-1\n"); } } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const int MaxN = 1e5 + 10; int n, u, v; int ans[MaxN][5]; int cnt[MaxN]; int vis[MaxN]; int d[MaxN]; bool dfs(int a, int b, int k) { d[k - 1] = a, d[k] = b; if (k == n) return b == 1; vis[b] = 1; for (int i = 1; i <= 4; i++) { int c = ans[b][i]; if (vis[c]) continue; if ((ans[a][1] == c || ans[a][2] == c || ans[a][3] == c || ans[a][4] == c) && dfs(b, c, k + 1)) return true; } vis[b] = 0; return 0; } int main() { scanf("%d", &n); for (int i = 1; i <= n * 2; i++) { scanf("%d%d", &u, &v); ans[u][++cnt[u]] = v; ans[v][++cnt[v]] = u; if (cnt[u] > 4 || cnt[v] > 4) { printf("-1\n"); return 0; } } for (int i = 1; i <= 4; i++) { if (dfs(1, ans[1][i], 1)) { for (int j = 0; j < n; j++) printf("%d ", d[j]); printf("\n"); return 0; } memset(d, 0, sizeof(d)); memset(vis, 0, sizeof(vis)); } printf("-1\n"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; vector<set<int>> g; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; g.assign(n, set<int>()); for (int i = 0; i < 2 * n; ++i) { int u, v; cin >> u >> v; u--, v--; g[u].insert(v); g[v].insert(u); } int ans = 1; for (int j = 0; j < n; ++j) { if (g[j].size() != 4) { cout << "-1\n"; return 0; } } if (n == 5) { cout << "1 2 3 4 5\n"; return 0; } vector<int> vis(n, 0); vector<int> A; if (n == 6) { vector<int> B(n); for (int i = 0; i < 3; ++i) { int u = -1; for (int j = 0; j < 6; ++j) { if (!vis[j]) u = j; } B[i] = u; vis[u] = 1; int v = -1; for (int k = 0; k < n; ++k) { if (k == u) continue; if (g[u].find(k) == g[u].end()) { if (v != -1) ans = 0; v = k; } } vis[v] = 1; B[i + 3] = v; } if (!ans) cout << "-1\n"; else for (int u : B) cout << u + 1 << " "; cout << endl; return 0; } A.push_back(0); vis[0] = 1; for (int k = 0; ans && k < n - 1; ++k) { pair<int, int> op = {-1, -1}; for (int u : g[A.back()]) { int cnt = 0; for (int v : g[u]) { if (g[A.back()].find(v) != g[A.back()].end()) cnt++; } if (cnt == 2 && op.first == -1) op.first = u; else if (cnt == 2) op.second = u; } if (op.second == -1) ans = 0; else if (!vis[op.first]) { vis[op.first] = 1; A.push_back(op.first); } else if (!vis[op.second]) { vis[op.second] = 1; A.push_back(op.second); } else ans = 0; } if (!ans) cout << "-1\n"; else for (int u : A) cout << u + 1 << " "; cout << endl; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> v[100005], kip[100005], sol; int chk[100005]; set<int> st; inline void cycle(int a) { sol.push_back(a); chk[a] = 1; if (chk[kip[a][0]] == 0) cycle(kip[a][0]); if (chk[kip[a][1]] == 0) cycle(kip[a][1]); return; } inline void PUT(int a) { int j, now; for (j = 0; j < v[a].size(); j++) { now = v[a][j]; st.insert(now); } return; } int main() { int n, k, a, b, i, j, flag; scanf("%d", &n); k = n * 2; for (i = 0; i < k; i++) { scanf("%d %d", &a, &b); a--; b--; v[a].push_back(b); v[b].push_back(a); } for (i = 0; i < n; i++) { if (v[i].size() != 4) { printf("-1\n"); return 0; } } if (n == 5) { for (i = 0; i < 5; i++) { memset(chk, 0, sizeof(chk)); chk[i]++; for (j = 0; j < v[i].size(); j++) chk[v[i][j]]++; flag = 0; for (j = 0; j < 5; j++) if (chk[j] != 1) flag = 1; if (flag == 1) { printf("-1\n"); return 0; } } printf("1 2 3 4 5\n"); } if (n == 6) { int p[10], a[10]; memset(p, -1, sizeof(p)); for (i = 0; i < 6; i++) { memset(chk, 0, sizeof(chk)); chk[i]++; for (j = 0; j < v[i].size(); j++) chk[v[i][j]]++; for (j = 0; j < 6; j++) { if (chk[j] > 0) continue; p[i] = j; } } for (i = 0; i < 6; i++) { if (i != p[p[i]]) { printf("-1\n"); return 0; } } int cnt; cnt = 0; for (i = 0; i < 6; i++) { if (p[i] == (-1)) continue; a[cnt] = i; a[cnt + 3] = p[i]; cnt++; p[p[i]] = -1; } for (i = 0; i < 6; i++) { if (i != 0) printf(" "); printf("%d", a[i] + 1); } printf("\n"); } if (n > 6) { int now; memset(kip, false, sizeof(kip)); for (i = 0; i < n; i++) { for (j = 0; j < 4; j++) { now = v[i][j]; st.clear(); PUT(i); PUT(now); if (st.size() == 6) kip[i].push_back(now); } if (kip[i].size() != 2) { printf("-1\n"); return 0; } } memset(chk, 0, sizeof(chk)); chk[0] = 1; sol.push_back(0); cycle(kip[0][0]); if (sol.size() == n) { for (i = 0; i < n; i++) { if (i != 0) printf(" "); printf("%d", sol[i] + 1); } printf("\n"); } else { printf("-1\n"); return 0; } } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
from sys import stdin all_in = stdin.readlines() n = int(all_in[0]) pairs = list(map(lambda x: tuple(map(int, x.split())), all_in[1:])) if n == 5: print(1, 2, 3, 4, 5) exit() neigs = {i: set() for i in range(1, n + 1)} for (a, b) in pairs: neigs[a].add(b) neigs[b].add(a) for el in neigs.values(): if len(el) != 4: print(-1) exit() ans = [1] used = {i: False for i in range(1, n + 1)} used[1] = True for i in range(n - 1): el_ = ans[-1] ne = neigs[el_] for el in ne: ne_ = neigs[el] and_ = ne & ne_ if len(and_) == 2: if i: if ans[-2] not in and_: continue if not used[el]: ans.append(el) used[el] = True break if len(ans) < n: print(-1) exit() print(' '.join(map(str, ans)))
PYTHON3
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.util.*; import java.io.*; public class Main { BufferedReader in; StringTokenizer str = null; PrintWriter out; private String next() throws Exception{ if (str == null || !str.hasMoreElements()) str = new StringTokenizer(in.readLine()); return str.nextToken(); } private int nextInt() throws Exception{ return Integer.parseInt(next()); } private long nextLong() throws Exception{ return Long.parseLong(next()); } private double nextDouble() throws Exception{ return Double.parseDouble(next()); } Set<Integer> []g; boolean used[]; int n, second; public void run() throws Exception{ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); n = nextInt(); g = new HashSet[n]; for(int i=0;i<n;i++) g[i] = new HashSet<Integer>(); for(int i=0;i<2*n;i++){ int x = nextInt()-1; int y = nextInt()-1; g[x].add(y); g[y].add(x); } for(int i=0;i<n;i++){ if (g[i].size() != 4){ out.println(-1); out.close(); return; } } used = new boolean[n]; boolean found = false; for(int i : g[0]){ Arrays.fill(used, false); used[0] = used[i] = true; second = i; if (build(0, i, 2)){ out.print((i+1) + " " + 1); out.println(); found = true; break; } } if (!found){ out.println(-1); } //out.println(ans.size()); //for(int i : ans) //out.print((i+1) + " "); //out.println(); out.close(); } private boolean build(int a, int b, int step) { //System.out.println(a + " " + b); if (step == n && g[a].contains(0) && g[b].contains(0) && g[b].contains(second)) { return true; } HashSet<Integer> s = new HashSet<Integer>(); for(int i : g[a]) if (!used[i]) s.add(i); for(int i : g[b]) if (!used[i]) s.add(i); used[a] = true; for(int i : s){ if (g[a].contains(i) && g[b].contains(i)){ if (build(b, i, step+1)){ out.print((i+1) + " "); return true; } } } used[a] = false; return false; } public static void main(String[] args) throws Exception{ new Main().run(); } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; map<int, int> m[100001]; int ans[100001], vis[100001], n; vector<int> v[100001]; void dfs(int pos) { if (pos == n) { if (m[ans[n - 1]].find(ans[1]) != m[ans[n - 1]].end() && m[ans[n]].find(ans[1]) != m[ans[n]].end() && m[ans[n]].find(ans[2]) != m[ans[n]].end()) { for (int i = 1; i <= n; i++) cout << ans[i] << " "; exit(0); } } for (int i = 0; i < v[ans[pos]].size(); i++) { int x = v[ans[pos]][i]; ans[pos + 1] = x; if (vis[x] || (pos > 1 && m[x].find(ans[pos - 1]) == m[x].end())) continue; vis[x] = 1; dfs(pos + 1); vis[x] = 0; } } int main() { int i, j, x, y; cin >> n; for (i = 1; i <= 2 * n; i++) { cin >> x >> y; v[x].push_back(y); ; v[y].push_back(x); m[x][y] = 1; m[y][x] = 1; } for (i = 1; i <= n; i++) { if (v[i].size() != 4) { cout << "-1"; return 0; } } ans[1] = 1; vis[1] = 1; dfs(1); cout << "-1"; return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author ocelopilli */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { ArrayList< ArrayList<Integer> > g = new ArrayList<ArrayList<Integer>>(); int[] ans; boolean[] seen; boolean solution(int i) { if ( i == ans.length ) return true; for (int x : g.get( ans[i-1] )) { if ( seen[x] == true ) continue; if ( g.get( ans[i-2] ).contains( x ) ) { seen[ ans[i] = x ] = true; return solution(i + 1); } } return false; } public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); for (int i=0; i<n; i++) g.add( new ArrayList<Integer>() ); for (int i=0; i<2*n; i++) { int a = in.nextInt() - 1; int b = in.nextInt() - 1; g.get( a ).add( b ); g.get( b ).add( a ); } boolean ok = true; for ( ArrayList<Integer> x : g ) if ( x.size() != 4 ) ok = false; if ( ok ) { ans = new int[ n ]; seen = new boolean[ n ]; ans[ 0 ] = 0; boolean found = false; for (int a : g.get( 0 )) for (int b : g.get( 0 )) if ( a!=b ) { ans[1] = a; ans[2] = b; Arrays.fill( seen, false ); for (int i=0; i<3; i++) seen[ ans[i] ] = true; if ( solution(3) ) { for (int i=0; i<n; i++) { if ( i > 0 ) out.print(" "); out.print( ans[i]+1 ); } out.println(); return; } } out.println( -1 ); } else out.println( -1 ); } } class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = null; } public String next() { while (st==null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:256000000") const int kN = 100 * 1000; vector<int> gr[kN]; int cnt[kN]; vector<int> Perm() { int a = 0; for (int i = 0; i < gr[a].size(); ++i) { int u = gr[a][i]; for (int j = 0; j < gr[u].size(); ++j) { if (gr[u][j] == a) continue; if (find(gr[a].begin(), gr[a].end(), gr[u][j]) != gr[a].end()) { vector<int> res; res.push_back(a); res.push_back(u); res.push_back(gr[u][j]); sort(res.begin(), res.end()); return res; } } } } bool used[kN]; int perm[kN]; int Intersection(int ind0, int ind1) { for (int i = 0; i < gr[ind0].size(); ++i) { if (find(gr[ind1].begin(), gr[ind1].end(), gr[ind0][i]) != gr[ind1].end() && !used[gr[ind0][i]]) { return gr[ind0][i]; } } return -1; } int main() { int n, m; cin >> n; m = 2 * n; for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; --u; --v; ++cnt[u]; ++cnt[v]; gr[u].push_back(v); gr[v].push_back(u); } for (int i = 0; i < n; ++i) if (cnt[i] != 4) { cout << -1; return 0; } vector<int> p = Perm(); bool isFind = false; do { memset(used, false, sizeof(used)); used[p[0]] = used[p[1]] = used[p[2]] = true; perm[0] = p[0]; perm[1] = p[1]; perm[2] = p[2]; int u; for (int i = 3; i < n; ++i) { u = Intersection(perm[i - 1], perm[i - 2]); if (u == -1) break; perm[i] = u; used[u] = true; } if (u == -1) continue; int lst = perm[n - 1]; if (find(gr[lst].begin(), gr[lst].end(), perm[0]) == gr[lst].end()) continue; if (find(gr[lst].begin(), gr[lst].end(), perm[1]) == gr[lst].end()) continue; isFind = true; break; } while (next_permutation(p.begin(), p.end())); if (!isFind) cout << -1; else for (int i = 0; i < n; ++i) cout << perm[i] + 1 << " "; return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int n, node[100005][5], ans[100005]; bool vis[100005]; bool ok(int k, int v) { if (!k) return true; for (int i = 1; i <= 4; i++) if (node[k][i] == v) return true; return false; } bool dfs(int u, int num) { if (num == n) return true; for (int i = 1; i <= 4; i++) { int v = node[u][i]; if (!vis[v] && ok(ans[num - 1], v)) { vis[v] = true; ans[num + 1] = v; if (dfs(v, num + 1)) return true; vis[v] = false; } } return false; } int main() { bool flag = true; int x, y; scanf("%d", &n); for (int i = 1; i <= 2 * n; i++) { scanf("%d %d", &x, &y); if (flag) { if (node[x][0] == 4 || node[y][0] == 4) flag = false; else { node[x][++node[x][0]] = y; node[y][++node[y][0]] = x; } } } if (flag) { memset(vis, false, sizeof(vis)); vis[1] = true; ans[1] = 1; if (dfs(1, 1)) for (int j = 1; j <= n; j++) printf("%d ", ans[j]); else printf("-1\n"); } else printf("-1\n"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.util.List; import java.io.IOException; import java.util.InputMismatchException; import java.util.ArrayList; import java.util.NoSuchElementException; import java.math.BigInteger; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Egor Kulikov ([email protected]) */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, InputReader in, OutputWriter out) { int count = in.readInt(); if (count == 5) { out.printLine("1 2 3 4 5"); return; } int[] from = new int[count * 2]; int[] to = new int[count * 2]; IOUtils.readIntArrays(in, from, to); MiscUtils.decreaseByOne(from, to); int[][] graph = GraphUtils.buildSimpleGraph(count, from, to); for (int i = 0; i < count; i++) { if (graph[i].length != 4) { out.printLine(-1); return; } } if (count == 6) { int[] reverse = new int[6]; for (int i = 0; i < 6; i++) { reverse[i] = 15 - i; for (int j : graph[i]) reverse[i] -= j; } int[] answer = new int[6]; int j = 0; for (int i = 0; i < 6; i++) { if (reverse[i] > i) answer[j++] = i + 1; } for (int i = 0; i < 3; i++) answer[i + 3] = reverse[answer[i] - 1] + 1; out.printLine(answer); return; } int[] newFrom = new int[count]; int[] newTo = new int[count]; int j = 0; for (int i = 0; i < count; i++) { for (int k : graph[i]) { if (k > i) continue; int same = 0; for (int l : graph[i]) { for (int m : graph[k]) { if (l == m) same++; } } // if (same > 2) { // out.printLine(-1); // return; // } if (same == 2) { if (j == count) { out.printLine(-1); return; } newFrom[j] = i; newTo[j++] = k; } } } if (j != count) { out.printLine(-1); return; } int[][] original = graph; graph = GraphUtils.buildSimpleGraph(count, newFrom, newTo); for (int i = 0; i < count; i++) { if (graph[i].length != 2) { out.printLine(-1); return; } } boolean[] visited = new boolean[count]; int current = 0; int[] answer = new int[count]; for (int i = 0; i < count; i++) { answer[i] = current + 1; visited[current] = true; if (i == count - 1) break; int next; if (visited[graph[current][0]]) next = graph[current][1]; else next = graph[current][0]; if (visited[next]) { out.printLine(-1); return; } current = next; } for (int i = 0; i < count; i++) { int next = i + 2; int next1 = i + 1; if (next >= count) next -= count; next = answer[next] - 1; if (next1 >= count) next1 -= count; next1 = answer[next1] - 1; boolean found = false; boolean found1 = false; for (int k : original[answer[i] - 1]) { if (k == next) found = true; if (k == next1) found1 = true; } if (!found || !found1) { out.printLine(-1); return; } } out.printLine(answer); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) writer.print(' '); writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } } class IOUtils { public static void readIntArrays(InputReader in, int[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) arrays[j][i] = in.readInt(); } } } class MiscUtils { public static void decreaseByOne(int[]...arrays) { for (int[] array : arrays) { for (int i = 0; i < array.length; i++) array[i]--; } } } class GraphUtils { public static int[][] buildGraph(int vertexCount, int[] from, int[] to) { int edgeCount = from.length; int[] degree = new int[vertexCount]; for (int i = 0; i < edgeCount; i++) { degree[from[i]]++; degree[to[i]]++; } int[][] graph = new int[vertexCount][]; for (int i = 0; i < vertexCount; i++) graph[i] = new int[degree[i]]; for (int i = 0; i < edgeCount; i++) { graph[from[i]][--degree[from[i]]] = i; graph[to[i]][--degree[to[i]]] = i; } return graph; } public static int otherVertex(int vertex, int from, int to) { return from + to - vertex; } public static int[][] buildSimpleGraph(int vertexCount, int[] from, int[] to) { int[][] graph = buildGraph(vertexCount, from, to); simplifyGraph(from, to, graph); return graph; } private static void simplifyGraph(int[] from, int[] to, int[][] graph) { for (int i = 0; i < graph.length; i++) { for (int j = 0; j < graph[i].length; j++) { graph[i][j] = otherVertex(i, from[graph[i][j]], to[graph[i][j]]); } } } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.PriorityQueue; import java.util.Random; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.lang.Long; import java.math.BigDecimal; import java.math.BigInteger; /** * Problem statement: http://codeforces.com/problemset/problem/431/D * * @author thepham * */ public class C_Round_161_Div2 { static long Mod = 1000000007; static int[][][] dp; static int max = 100000; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int n = in.nextInt(); HashSet<Integer>[]map = new HashSet[n]; for(int i = 0; i < n; i++){ map[i] = new HashSet(); } for(int i = 0; i < 2*n; i++){ int a = in.nextInt() - 1; int b = in.nextInt() - 1; map[a].add(b); map[b].add(a); } if(n == 5){ out.println("1 2 3 4 5"); }else if (n == 6){ int[]data = {0,1,2,3,4,5}; boolean ok = false; do{ ok = true; for(int i = 0; i < n; i++){ int x = (i - 1 + n)%n; int y = (i + 1)%n; if(!map[data[i]].contains(data[x]) || !map[data[i]].contains(data[y]) || !map[data[x]].contains(data[y]) || !map[data[y]].contains(data[x])){ ok = false; break; } } if(ok){ for(int i : data){ out.print((i + 1) + " "); } break; } }while(nextPer(data)); if(!ok){ out.println(-1); } }else{ int[]result = new int[n]; Arrays.fill(result, -1); int nxt = 0; result[0] = 0; boolean ok = true; //System.out.println(Arrays.toString(map)); for(int i = 0; i < n - 1 && ok; i++){ if(map[nxt].size() != 4){ ok = false; break; } int u = -1; int v = -1; //System.out.println(nxt); for(int j : map[nxt]){ int count = 0; for(int k : map[nxt]){ if(j != k){ if(map[j].contains(k)){ count++; } } } //System.out.println(count + " " + j); if(count == 2){ if(u == -1){ u = j; }else if(v == -1){ v = j; }else{ ok = false; break; } }else if(count != 1){ ok = false; break; } } if(u == -1 || v == -1){ ok = false; break; } if(i == 0){ result[i + 1] = u; result[n - 1] = v; nxt = u; }else{ if(result[i - 1] == u){ if(result[i + 1] == v || result[i + 1] == -1){ result[i + 1] = v; nxt = v; }else{ ok = false; break; } }else if(result[i - 1] == v){ if(result[i + 1] == u || result[i + 1] == -1){ result[i + 1] = u; nxt = u; }else{ ok = false; break; } }else{ ok = false; break; } } } //System.out.println(Arrays.toString(result)); if(ok){ for(int i : result){ out.print((i + 1) + " "); } }else{ out.println(-1); } } out.close(); } static long min(long a, long b){ if(a > b){ return b; } return a; } public static int[] buildKMP(String val) { int[] data = new int[val.length() + 1]; int i = 0; int j = -1; data[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(i) != val.charAt(j)) { j = data[j]; } i++; j++; data[i] = j; // System.out.println(val + " " + i + " " + data[i]); } return data; } static int find(int index, int[] u) { if (u[index] != index) { return u[index] = find(u[index], u); } return index; } static int crossProduct(Point a, Point b) { return a.x * b.y + a.y * b.x; } static long squareDist(Point a) { long x = a.x; long y = a.y; return x * x + y * y; } static Point minus(Point a, Point b) { return new Point(a.x - b.x, a.y - b.y); } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } static class Point { int x, y; public Point(int x, int y) { super(); this.x = x; this.y = y; } public String toString() { return "{" + x + " " + y + "}"; } } static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static class FT { int[] data; FT(int n) { data = new int[n]; } public void update(int index, int value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public int get(int index) { int result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new // BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new // FileInputStream( // new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> cnt(n); vector<set<int>> g(n); for (int i = 0; i < 2 * n; ++i) { int u, v; cin >> u >> v; --u, --v; g[u].insert(v); g[v].insert(u); cnt[v]++; cnt[u]++; } for (auto elem : cnt) { if (elem != 4) { cout << -1; return 0; } } vector<pair<int, int>> tri; for (auto u : g[0]) { for (auto v : g[0]) { if (u != v && g[u].count(v) == 1) { tri.push_back({u, v}); } } } for (auto elem : tri) { int v0 = elem.first; vector<int> cycle; cycle.push_back(0); int u0 = elem.second; cycle.push_back(v0); cycle.push_back(u0); vector<int> used(n); used[0] = 1; used[v0] = 1; used[u0] = 1; bool ok = true; while (ok && cycle.size() < n) { ok = false; int v1 = -1; for (auto u : g[cycle.back()]) { if (g[u].count(cycle[cycle.size() - 2]) && used[u] == 0) { v1 = u; ok = true; } } if (v1 != -1) { used[v1] = 1; cycle.push_back(v1); } } if (n == cycle.size()) { for (auto elem : cycle) { cout << elem + 1 << ' '; } return 0; } } cout << -1; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int n, dem, d[100010], kq[100010], dd[100010]; vector<int> a[100010]; void nhap() { cin >> n; int u, v; for (int i = 1; i <= 2 * n; i++) { scanf("%d%d", &u, &v); a[u].push_back(v); a[v].push_back(u); } } void dfs(int x, int y, int z) { int ok; d[x] = y; dd[y] = 1; if (x == n - 1) dem = 1; for (int i = 0; i < 4; i++) if (!dd[a[y][i]]) { ok = 0; for (int j = 0; j < 4; j++) if (a[z][j] == a[y][i]) { ok = 1; break; } if (ok) dfs(x + 1, a[y][i], y); } } void cbi(int i, int j) { memset(d, 0, sizeof(d)); memset(dd, 0, sizeof(dd)); dem = 0; d[1] = 1; d[2] = a[1][i]; d[n] = a[1][j]; dd[d[2]] = dd[d[n]] = dd[1] = 1; } void xuli() { for (int i = 1; i <= n; i++) if (a[i].size() != 4) { cout << -1; exit(0); } for (int i = 0; i < 4; i++) for (int j = i + 1; j < 4; j++) { cbi(i, j); dfs(2, d[2], 1); if (dem) for (int k = 1; k <= n; k++) kq[k] = d[k]; } } void ghikq() { if (kq[1]) for (int i = 1; i <= n; i++) printf("%d ", kq[i]); else cout << -1; } int main() { nhap(); xuli(); ghikq(); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int n, a, b, ans[100100], used[100100]; vector<int> adj[100100]; bool con(int a, int b) { int i; if (a == 0 || b == 0) return true; for (i = 0; i < 4; i++) if (adj[a][i] == b) return true; return false; } void dfs(int p) { int i; if (p == n) { if (!(con(ans[n - 1], ans[1]) && con(ans[n], ans[1]) && con(ans[n], ans[2]))) return; for (i = 1; i <= n; i++) printf("%d ", ans[i]); exit(0); } for (i = 0; i < 4; i++) { ans[p + 1] = adj[ans[p]][i]; if (used[ans[p + 1]] || !con(ans[p + 1], ans[p - 1])) continue; used[ans[p + 1]] = 1; dfs(p + 1); used[ans[p + 1]] = 0; } } int main() { int i; scanf("%d", &n); for (i = 0; i < 2 * n; i++) { scanf("%d%d", &a, &b); adj[a].push_back(b); adj[b].push_back(a); } for (i = 1; i <= n; i++) { if (adj[i].size() != 4) { puts("-1"); return 0; } } used[1] = 1; ans[1] = 1; dfs(1); puts("-1"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; bool ok = 1; bool vis[100001] = {0}; vector<int> ng[100001], g[100001]; void print(int v, bool p) { vis[v] = 1; if (p) printf("%d ", v); ok &= ng[v].size() == 2; for (int i = 0; i < ng[v].size(); ++i) { int u = ng[v][i]; if (!vis[u]) print(u, p); } } inline bool exist(vector<int>& a, int v) { bool ok = 0; for (int i = 0; i < a.size(); ++i) ok |= a[i] == v; return ok; } int main() { int n; scanf("%d", &n); for (int i = 0, m = 2 * n; i < m; ++i) { int a, b; scanf("%d %d", &a, &b); g[a].push_back(b), g[b].push_back(a); } if (n <= 6) { ok = 0; int v[11]; for (int i = 1; i <= n; ++i) v[i - 1] = i; do { int c = 0; for (int i = 0; i < n; ++i) for (int d = -2; d < 3; ++d) c += exist(g[v[i]], v[(i + d + n) % n]); if (c == 4 * n) { ok = 1; break; } } while (next_permutation(v, v + n)); if (ok) { for (int i = 0; i < n; ++i) printf("%d ", v[i]); } } else { for (int i = 1; i <= n; ++i) sort(g[i].begin(), g[i].end()); ok = 1; int t = 0; for (int i = 1; i <= n && ok; ++i) { ok &= g[i].size() == 4; int sum = 0; vector<int>& q = g[i]; for (int j = 0; j < q.size() && ok; ++j) { vector<int>& r = g[q[j]]; int c = 0; for (int k = 0, l = 0; k < q.size() && l < q.size();) { if (q[k] == r[l]) ++c, ++k, ++l; else if (q[k] < r[l]) ++k; else ++l; } if (c == 2) ng[i].push_back(q[j]), ++t; ok &= c == 2 || c == 1; sum += c; } ok &= sum == 6; } ok &= t == 2 * n; print(1, 0); for (int i = 1; i <= n && ok; ++i) ok &= vis[i]; memset(vis, 0, sizeof vis); if (ok) print(1, 1); } if (!ok) printf("-1"); puts(""); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int A[100001][5]; int C[100001]; vector<int> V; int n, flag; void solve(int indx) { int i, sz, temp[4], a, j; if (flag) return; if (A[indx][0] != 4) { flag = 1; printf("-1\n"); return; } sz = V.size(); if (V.size() == n) { for (i = 0; i < n; i++) { a = i - 1; if (a < 0) a += n; temp[0] = V[a]; a = i - 2; if (a < 0) a += n; temp[1] = V[a]; a = i + 1; if (a > n - 1) a -= n; temp[2] = V[a]; a = i + 2; if (a > n - 1) a -= n; temp[3] = V[a]; sort(temp, temp + 4); for (j = 0; j < 4; j++) { if (temp[j] != A[V[i]][j + 1]) return; } } for (i = 0; i < n; i++) printf("%d ", V[i]); flag = 1; return; } for (i = 1; i <= 4; i++) { sz = V.size(); if (!C[A[indx][i]] && (sz == 1 || A[indx][i] == A[V[sz - 2]][1] || A[indx][i] == A[V[sz - 2]][2] || A[indx][i] == A[V[sz - 2]][3] || A[indx][i] == A[V[sz - 2]][4])) { C[A[indx][i]] = 1; V.push_back(A[indx][i]); solve(A[indx][i]); V.pop_back(); C[A[indx][i]] = 0; } } } int main() { int i, a, b, j; flag = 0; memset(A, 0, sizeof(A)); memset(C, 0, sizeof(C)); V.clear(); scanf("%d", &n); for (i = 0; i < 2 * n; i++) { scanf("%d%d", &a, &b); A[a][0]++; if (A[a][0] > 4) { printf("-1\n"); return 0; } A[a][A[a][0]] = b; A[b][0]++; if (A[b][0] > 4) { printf("-1\n"); return 0; } A[b][A[b][0]] = a; } for (i = 1; i <= n; i++) sort(A[i] + 1, A[i] + 5); C[1] = 1; V.push_back(1); solve(1); if (!flag) printf("-1\n"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const long long OO = 1e9; int n; vector<int> c[100005]; int vis[100005]; int ans[100005]; int cot[100005]; bool _find(int x, int cc) { for (int i = 0; i < c[x].size(); i++) if (cc == c[x][i]) return 1; return 0; } bool dfs(int x, int indx) { if (indx == n) return true; vis[x] = 1; ans[indx] = x; bool ok = false; for (int i = 0; i < c[x].size(); i++) { int y = c[x][i]; if (indx == n - 1) { if (_find(ans[0], x) && _find(ans[1], x) && _find(ans[0], ans[indx - 1])) ok = true; } else if (!vis[y] && (x == 1 || _find(y, ans[indx - 1])) && !ok) { ok = dfs(y, indx + 1); } if (ok) break; } vis[x] = 0; return ok; } int main() { int x, y; cin >> n; bool ok = true; for (int i = 0; i < 2 * n; i++) { scanf("%d %d", &x, &y); c[x].push_back(y); c[y].push_back(x); cot[x]++; cot[y]++; if (cot[x] > 4 || cot[y] > 4) ok = false; } if (ok) ok = dfs(1, 0); if (!ok) cout << -1 << endl; else { for (int i = 0; i < n; i++) printf("%d ", ans[i]); cout << endl; } }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.io.*; import java.util.*; import java.util.StringTokenizer; public class SolutionC { BufferedReader in; StringTokenizer str; PrintWriter out; String SK; String next() throws IOException { while ((str == null) || (!str.hasMoreTokens())) { SK = in.readLine(); if (SK == null) return null; str = new StringTokenizer(SK); } return str.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); //in = new BufferedReader(new FileReader("input.txt")); //out = new PrintWriter("output.txt"); solve(); out.close(); } public static void main(String[] args) throws IOException { new SolutionC().run(); } void solve() throws IOException { int n=nextInt(); int point[][]=new int[n+1][4]; int c[]=new int[n+1]; boolean okk=false; for(int i=0;i<2*n;i++){ int a=nextInt(); int b=nextInt(); if(c[a]==4 || c[b]==4){ okk=true; } else{ point[a][c[a]++] = b; point[b][c[b]++]=a; } } if(okk){ out.println(-1); return; } for(int i=1;i<=n;i++){ if(c[i]!=4){ out.println(-1); return; } } if(n==5){ out.println("1 2 3 4 5"); return; } if(n==6){ int cc=0; int pair[][]=new int[3][2]; boolean used[]=new boolean[7]; for(int curP=1;curP<=6;curP++){ if(used[curP])continue; int a[]=new int[7]; a[curP]=1; for(int neig=0;neig<4;neig++){ a[point[curP][neig]]=1; } for(int i=1;i<=6;i++){ if(a[i]==0){ pair[cc][0]=curP; pair[cc++][1]=i; used[curP]=true; used[i]=true; break; } } } out.print(pair[0][0]+" "+pair[1][0]+" "); out.print(pair[2][0]+" "+pair[0][1]+" "); out.print(pair[1][1]+" "+pair[2][1]); out.println(); return; } ArrayList<Integer> res = new ArrayList<Integer>(); res.add(1); int curP=1; boolean used[]=new boolean[n+1]; used[1]=true; while(true){ int tt=curP; for(int neig=0;neig<4;neig++){ int temp=point[curP][neig]; TreeSet<Integer> inter = new TreeSet<Integer>(); for(int k=0;k<4;k++){ inter.add(point[curP][k]); } for(int k=0;k<4;k++){ inter.add(point[temp][k]); } if(inter.size()==6 && !used[temp]){ res.add(temp); curP=temp; used[temp]=true; break; } } if(res.size()==n){ break; } if(tt==curP){ out.println(-1); return; } } int t=res.size(); for(int i=0;i<t-1;i++){ out.print(res.get(i)+" "); } out.println(res.get(t-1)); } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.io.*; import java.util.*; public class C { void run() throws IOException { int n = ni(), m = 2 * n; int[][] a = new int[n][4]; int[] b = new int[n]; boolean[] c = new boolean[n]; for (int i = 0; i < m; i++) { int x = ni() - 1, y = ni() - 1; if (b[x] == 4 || b[y] == 4) { pw.print(-1); return; } a[x][b[x]++] = y; a[y][b[y]++] = x; } if (n == 5) { pw.print("1 2 3 4 5"); return; } if (n == 6) { HashSet[] hm = new HashSet[6]; for (int i = 0; i < n; i++) { hm[i] = new HashSet<Integer>(); for (int j = 0; j < 4; j++) hm[i].add(a[i][j]); } for (int a1 = 0; a1 < 6; a1++) for (int a2 = 0; a2 < 6; a2++) for (int a3 = 0; a3 < 6; a3++) for (int a4 = 0; a4 < 6; a4++) for (int a5 = 0; a5 < 6; a5++) for (int a6 = 0; a6 < 6; a6++) { if (a1 != a2 && a1 != a3 && a1 != a4 && a1 != a5 && a1 != a6) { if (a2 != a3 && a2 != a4 && a2 != a5 && a2 != a6) { if (a3 != a4 && a3 != a5 && a3 != a6) { if (a4 != a5 && a4 != a6) { if (a5 != a6) { boolean z = true; z &= (!hm[a1].contains(a4)); z &= (!hm[a2].contains(a5)); z &= (!hm[a3].contains(a6)); z &= (!hm[a4].contains(a1)); z &= (!hm[a5].contains(a2)); z &= (!hm[a6].contains(a3)); if (z) { pw.println(++a1 + " " + ++a2 + " " + ++a3 + " " + ++a4 + " " + ++a5 + " " + ++a6); return; } } } } } } } pw.print(-1); return; } StringBuilder sb = new StringBuilder(); int q = 1; int last = 0; sb.append(1); sb.append(' '); c[0] = true; // pw.print("1 "); for (int i = 1; i < n; i++) { for (int j = 0; j < 4; j++) { int next = a[last][j]; if (!c[next]) { HashSet<Integer> hm = new HashSet<Integer>(); int z = 0; for (int k = 0; k < 4; k++) { hm.add(a[last][k]); } for (int k = 0; k < 4; k++) { if (hm.contains(a[next][k])) z++; } if (z == 2) { sb.append(1 + next); sb.append(' '); q++; c[next] = true; last = next; break; } } } } if (q == n) pw.print(sb.toString()); else pw.print(-1); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } String nl() throws IOException { return br.readLine(); } PrintWriter pw; BufferedReader br; StringTokenizer st; public static void main(String[] args) throws IOException { long timeout = System.currentTimeMillis(); boolean CF = System.getProperty("ONLINE_JUDGE") != null; PrintWriter _pw = new PrintWriter(System.out); BufferedReader _br = new BufferedReader(CF ? new InputStreamReader(System.in) : new FileReader(new File("in.txt"))); new C(_br, _pw).run(); if (!CF) { _pw.println(); _pw.println(System.currentTimeMillis() - timeout); } _br.close(); _pw.close(); } public C(BufferedReader _br, PrintWriter _pw) { br = _br; pw = _pw; } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; pair<int, int> ke[100005]; vector<int> a[100005], res; map<pair<int, int>, int> mm; int n, sl[100005], b[100005]; bool dd[100005]; void process() { for (int i = (1); i <= (n); i++) if (a[i].size() != 4) { cout << -1 << endl; exit(0); } for (int i = (1); i <= (n); i++) { for (int j = 0; j < (4); j++) sl[a[i][j]] = 0; for (int j = 0; j < (4); j++) { for (int k = (j + 1); k <= (3); k++) { int u = a[i][j], v = a[i][k]; if (u > v) swap(u, v); if (mm.find(make_pair(u, v)) != mm.end()) { sl[u]++; sl[v]++; } } } for (int j = 0; j < (4); j++) { for (int k = (j + 1); k <= (3); k++) { int u = a[i][j], v = a[i][k]; if (sl[u] == 2 && sl[v] == 2) { ke[i] = make_pair(u, v); } } } } for (int i = (1); i <= (n); i++) if (ke[i] == make_pair(0, 0)) { cout << -1 << endl; exit(0); } } void ghi() { res.push_back(1); dd[1] = true; int cur = 1; while (res.size() < n) { int u = ke[cur].first, v = ke[cur].second; if (!dd[u]) { res.push_back(u); dd[u] = true; cur = u; } else if (!dd[v]) { res.push_back(v); dd[v] = true; cur = v; } else { cout << -1 << endl; exit(0); } } for (int i = 0; i < (res.size()); i++) printf("%d ", res[i]); } bool check() { for (int i = (1); i <= (n); i++) { int u = b[i], v = b[i + 1]; if (i == n) v = b[1]; if (u > v) swap(u, v); if (mm.find(make_pair(u, v)) == mm.end()) return false; u = b[i]; v = b[i + 2]; if (i >= n - 1) v = b[2 + i - n]; if (u > v) swap(u, v); if (mm.find(make_pair(u, v)) == mm.end()) return false; } return true; } void duyet(int i) { if (i > n) { if (check()) { for (int j = (1); j <= (n); j++) cout << b[j] << ' '; cout << endl; exit(0); } return; } for (int j = (2); j <= (n); j++) if (!dd[j]) { dd[j] = true; b[i] = j; duyet(i + 1); dd[j] = false; } } void xulirieng() { b[1] = 1; duyet(2); cout << -1 << endl; } int main() { cin >> n; for (int i = (1); i <= (2 * n); i++) { int u, v; scanf("%d%d", &u, &v); if (u > v) swap(u, v); mm[make_pair(u, v)] = 1; a[u].push_back(v); a[v].push_back(u); } if (n <= 6) xulirieng(); else { process(); ghi(); } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> deg; vector<vector<int> > adj; set<long long> mat; int N, depth = 0; int vis[(int)1e5 + 5]; bool dfs(int f, int s, int t) { if (depth == N - 3) return 1; depth++; for (int j = 0; j < ((int)adj[t].size()); j++) { int nxt = adj[t][j]; if (mat.count(1LL * nxt * N + s) && !vis[nxt]) { vis[nxt]++; if (dfs(s, t, nxt)) { cout << nxt + 1 << " "; return 1; } return 0; } } return 0; } int main() { ios_base::sync_with_stdio(false); int a, b; cin >> N; deg.resize(N, 0); adj.resize(N); for (int i = 0; i < N * 2; i++) { cin >> a >> b; a--, b--; deg[a]++, deg[b]++; if (deg[a] > 4 || deg[b] > 4) { cout << -1; return 0; } mat.insert(1LL * a * N + b); mat.insert(1LL * b * N + a); adj[a].push_back(b); adj[b].push_back(a); } for (int i = 0; i < ((int)adj[0].size()); i++) for (int j = 0; j < ((int)adj[0].size()); j++) { a = adj[0][i], b = adj[0][j]; if (a == b) continue; memset(vis, 0, (sizeof(int)) * N); depth = 0, vis[0] = vis[a] = vis[b] = 1; if (dfs(0, a, b)) { cout << b + 1 << " " << a + 1 << " " << 1; return 0; } } cout << -1; return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int n, a, b, bylo[100007], uzyte[100007], t[100007]; vector<int> v[100007]; bool mozna; bool check(int x) { bylo[t[(x - 1 + n) % n]] = 1; bylo[t[(x - 2 + n) % n]] = 1; bylo[t[(x + 1) % n]] = 1; bylo[t[(x + 2) % n]] = 1; for (__typeof((v[t[x]]).begin()) it = ((v[t[x]]).begin()); it != (v[t[x]]).end(); it++) --bylo[*it]; for (__typeof((v[t[x]]).begin()) it = ((v[t[x]]).begin()); it != (v[t[x]]).end(); it++) if (bylo[*it] != 0) return false; return true; } int main() { scanf("%d", &n); for (int i = 0; i < (2 * n); i++) { scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); } mozna = true; for (int i = 1; i <= (n); i++) if (int(v[i].size()) != 4) mozna = false; if (!mozna) { puts("-1"); return 0; } for (int k = 0; k < (24); k++) { t[0] = v[1][0]; t[1] = v[1][1]; t[2] = 1; t[3] = v[1][2]; t[4] = v[1][3]; for (int i = 0; i < (5); i++) uzyte[t[i]] = 1; mozna = true; for (int i = 3; i <= (n - 3); i++) { bylo[t[i - 1]] = 1; bylo[t[i - 2]] = 1; bylo[t[i + 1]] = 1; for (__typeof((v[t[i]]).begin()) it = ((v[t[i]]).begin()); it != (v[t[i]]).end(); it++) --bylo[*it]; if (bylo[t[i - 1]] == 0 && bylo[t[i - 2]] == 0 && bylo[t[i + 1]] == 0) { for (__typeof((v[t[i]]).begin()) it = ((v[t[i]]).begin()); it != (v[t[i]]).end(); it++) if (bylo[*it] == -1 && !uzyte[*it]) { bylo[*it] = 0; uzyte[*it] = 1; t[i + 2] = *it; } if (t[i + 2] == 0) mozna = false; } else mozna = false; if (mozna == false) break; } if (mozna) { for (int i = 0; i < (n); i++) if (!check(i)) mozna = false; if (mozna) { for (int i = 0; i < (n); i++) printf("%d ", t[i]); puts(""); return 0; } } for (int i = 0; i <= (n); i++) t[i] = bylo[i] = uzyte[i] = 0; next_permutation(v[1].begin(), v[1].end()); } puts("-1"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import sys, array #, time #S = time.time() readInts = lambda : map(int, raw_input().split()) #readArgs = lambda : raw_input().split() #write = lambda *s: sys.stdout.write(' '.join(map(str, s))) #sys.stdin = open('data.txt', 'r') #sys.stdout = open('ans.txt', 'w') n = readInts()[0] if n == 5: print 1, 2, 3, 4, 5 sys.exit(0) near = [ [] ] nextTo = [ [] ] ok = [ True ] #T = time.time() for _ in xrange(n): near.append([]) nextTo.append([]) ok.append(True) IN = map(int, sys.stdin.read().split()) #getInts = lambda : (IN.pop(), IN.pop()) #print >>sys.stderr, 'OK!' #print >>sys.stderr, 'Init Time = %.3f s' % (time.time() - T) #T = time.time() #for _ in range(n): while True: try: a = IN.pop() b = IN.pop() except: break near[a].append(b) near[b].append(a) #print >>sys.stderr, 'OK!' #print >>sys.stderr, 'Read Data Time = %.3f s' % (time.time() - T) #T = time.time() if n == 6: link = {} ans = [0, 0, 0, 0, 0, 0] t = set([1, 2, 3, 4, 5, 6]) for i in xrange(1, 7): try: link[i] = (t - set(near[i] + [i])).pop() except: print -1 sys.exit(0) cur = 0 for i in link: if ans.count(i): continue ans[cur] = i ans[cur + 3] = link[i] cur = cur + 1 print ' '.join(map(str, ans)) sys.exit(0) for i in xrange(1, n + 1): cnt = 0 if len(near[i]) ^ 4: print -1 sys.exit(0) for j in near[i]: if len(set(near[i]).union(near[j])) == 6: nextTo[i].append(j) cnt = cnt + 1 #c = 0 #for k in near[i]: # if near[j].count(k): # c = c + 1 #if c == 2: # nextTo[i].append(j) # cnt = cnt + 1 if cnt ^ 2: print -1 sys.exit(0) #print >>sys.stderr, 'OK!' #print >>sys.stderr, 'Process Time = %.3f s' % (time.time() - T) #T = time.time() ans = [ 1 ] cur = 1 ok[1] = False for i in xrange(n - 1): j = nextTo[cur].pop() if ok[j]: nextTo[j].remove(cur) cur = j ans.append(cur) ok[cur] = False continue print -1 sys.exit(0) #print >>sys.stderr, 'OK!' #print >>sys.stderr, 'Finalize Time = %.3f s' % (time.time() - T) #print >>sys.stderr, '' #print >>sys.stderr, 'Total Time = %.3f s' % (time.time() - S) sys.stdout.write(' '.join(map(str, ans)))
PYTHON
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class Task161C { static PrintWriter output; static int[][] points = new int[100000+1][4]; /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { output = new PrintWriter(System.out); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); //int[][] points = new int[n+1][4]; for (int i=0; i<2*n; i++) { int p1 = sc.nextInt(); int p2 = sc.nextInt(); if (points[p1][0] == 0) { points[p1][0] = p2; } else if (points[p1][1] == 0) { points[p1][1] = p2; } else if (points[p1][2] == 0) { points[p1][2] = p2; } else if (points[p1][3] == 0) { points[p1][3] = p2; } if (points[p2][0] == 0) { points[p2][0] = p1; } else if (points[p2][1] == 0) { points[p2][1] = p1; } else if (points[p2][2] == 0) { points[p2][2] = p1; } else if (points[p2][3] == 0) { points[p2][3] = p1; } } int[] result = new int[n+1]; boolean [] used = new boolean[n+1]; result[0] = 1; int current = 1; used[current] = true; int k = 1; while (k < n) { int next = 0; for (int i=0; i<4; i++) { for (int j=0; j<4; j++) { for (int l=0; l<4; l++) { if (i!=j && j!=l && connected(points[current][i], points[current][j]) && connected(points[current][i], points[current][l]) && (n==5 || !connected(points[current][l], points[current][j]))&& !used[points[current][i]] && (k < 2 || connected(result[k-2], points[current][i]))) { next = points[current][i]; break; } } if (next > 0) break; } if (next > 0) break; } if (next == 0) { output.write("-1"); output.close(); return; } result[k] = next; used[next] = true; current = next; k++; } if (!(connected(result[0], result[n-1]) && connected(result[1], result[n-1]) && connected(result[0], result[n-2]))) { output.write("-1"); output.close(); return; } for (int i=0; i<n; i++) { output.write("" + result[i] + " "); } output.close(); } private static boolean connected(int p1, int p2) { return (points[p1][0] == p2 || points[p1][1] == p2 || points[p1][2] == p2 || points[p1][3] == p2 || points[p2][0] == p1 || points[p2][1] == p1 || points[p2][2] == p1 || points[p2][3] == p1); } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; map<pair<int, int>, int> mp; vector<int> v[maxn << 1]; int cnt[maxn], sta[maxn], tot; bool vis[maxn]; int n; int dfs(int fa, int u, int t) { if (vis[t]) return 0; sta[++tot] = t; if (tot == n) return 1; vis[u] = 1; for (int i = 0; i < 4; i++) { int e = v[t][i]; if (e == fa || e == u || !mp[make_pair(u, e)]) continue; if (dfs(u, t, e)) return 1; } return 0; } int main() { int a, b; scanf("%d", &n); for (int i = 1; i <= n * 2; i++) { scanf("%d%d", &a, &b); mp[make_pair(a, b)] = 1; mp[make_pair(b, a)] = 1; cnt[a]++; cnt[b]++; v[a].push_back(b); v[b].push_back(a); } for (int i = 1; i <= n; i++) if (cnt[i] != 4) { puts("-1"); return 0; } if (n == 5) { puts("1 2 3 4 5"); return 0; } for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) { if (i == j) continue; int e1 = v[1][i], e2 = v[1][j]; if (mp[make_pair(e1, e2)]) { tot = 1; for (int k = 1; k <= n; k++) vis[k] = 0; vis[1] = 1; sta[1] = 1; vis[e1] = 1; sta[++tot] = e1; if (dfs(1, e1, e2)) { for (int k = 1; k <= n; k++) printf("%d ", sta[k]); puts(""); return 0; } vis[e1] = 0; tot--; } } puts("-1"); }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
//package round161; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class C { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(); int[] from = new int[2*n]; int[] to = new int[2*n]; for(int i = 0;i < 2*n;i++){ from[i] = ni()-1; to[i] = ni()-1; } if(n == 5){ out.println("1 2 3 4 5"); return; } if(n == 6){ boolean[][] q = new boolean[6][6]; for(int i = 0;i < 2*n;i++){ q[from[i]][to[i]] = q[to[i]][from[i]] = true; } for(int i = 0;i < n;i++){ q[i][i] = true; } int[] o = new int[n]; for(int i = 0;i < n;i++){ int op = -1; for(int j = 0;j < n;j++){ if(!q[i][j]){ if(op == -1){ op = j; }else{ out.println(-1); return; } } } o[i] = op; } int[] ret = new int[n]; int p = 0; for(int i = 0;i < n;i++){ if(i < o[i]){ ret[p] = i; ret[p+3] = o[i]; p++; } } for(int i = 0;i < n;i++){ if(i > 0)out.print(" "); out.print(ret[i]+1); } out.println(); return; } int[][] g = packU(n, from, to); for(int[] r : g){ if(r.length != 4){ out.println(-1); return; } Arrays.sort(r); } boolean[] ved = new boolean[n]; int[] hist = new int[n]; int cur = 0; outer: for(int step = 0;step < n;step++){ ved[cur] = true; hist[step] = cur; if(step == n-1)break; for(int x : g[cur]){ if(!ved[x] && eq(g[x], g[cur]) == 2){ cur = x; continue outer; } } out.println(-1); return; } for(int i = 0;i < n;i++){ if(i > 0)out.print(" "); out.print(hist[i]+1); } out.println(); // // check // for(int i = 0;i < n;i++){ // int[] u = new int[4]; // u[0] = hist[(i+1)%n]; // u[1] = hist[(i+2)%n]; // u[2] = hist[(i+n-1)%n]; // u[3] = hist[(i+n-2)%n]; // Arrays.sort(u); // if(!Arrays.equals(u, g[hist[i]])){ // tr("break", i); // } // } } static int eq(int[] a, int[] b) { int ct = 0; for(int p = 0, q = 0;p < 4 && q < 4;){ if(a[p] == b[q]){ ct++; p++; q++; }else if(a[p] < b[q]){ p++; }else if(a[p] > b[q]){ q++; } } return ct; } static int[][] packU(int n, int[] from, int[] to) { int[][] g = new int[n][]; int[] p = new int[n]; for(int f : from) p[f]++; for(int t : to) p[t]++; for(int i = 0;i < n;i++) g[i] = new int[p[i]]; for(int i = 0;i < from.length;i++){ g[from[i]][--p[from[i]]] = to[i]; g[to[i]][--p[to[i]]] = from[i]; } return g; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new C().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> e[100010]; int bfs[100010], v[100010], used[100010], an[100010], ha[100010], n, tt; bool con(int x, int y) { for (int i = 0; i < e[x].size(); i++) { if (e[x][i] == y) return 1; } return 0; } bool check() { int i; for (i = 0; i < n; i++) { if (i + 1 < n) { if (!con(an[i], an[i + 1])) return 0; } if (i + 2 < n) { if (!con(an[i], an[i + 2])) return 0; } } if (!con(an[0], an[n - 1])) return 0; if (!con(an[0], an[n - 2])) return 0; if (!con(an[1], an[n - 1])) return 0; return 1; } void go() { int i, j; tt++; ha[an[0]] = ha[an[1]] = ha[an[2]] = tt; for (i = 3; i < n; i++) { int x = an[i - 1]; for (j = 0; j < e[x].size(); j++) { if (con(e[x][j], an[i - 2]) && e[x][j] != an[i - 3]) { if (ha[e[x][j]] == tt) { return; } an[i] = e[x][j]; ha[e[x][j]] = tt; break; } } } if (!check()) { return; } for (i = 0; i < n; i++) { if (i) printf(" "); printf("%d", an[i]); } puts(""); exit(0); } int main() { int i, j, k; scanf("%d", &n); for (i = 0; i < n * 2; i++) { int x, y; scanf("%d%d", &x, &y); e[x].push_back(y); e[y].push_back(x); } for (i = 1; i <= n; i++) { if (e[i].size() != 4) { puts("-1"); return 0; } } if (n == 5) { for (i = 1; i <= 5; i++) { if (i != 1) printf(" "); printf("%d", i); } puts(""); return 0; } bfs[0] = 1; used[1] = 1; for (i = 0, j = 1; i < j; i++) { int x = bfs[i]; for (k = 0; k < e[x].size(); k++) { int y = e[x][k]; if (!used[y]) { used[y] = 1; bfs[j++] = y; v[y] = v[x] + 1; } } } vector<int> one; for (i = 1; i <= n; i++) { if (v[i] == 1) one.push_back(i); } if (one.size() != 4) { puts("-1"); return 0; } for (i = 0; i < one.size(); i++) { for (j = i + 1; j < one.size(); j++) { if (con(one[i], one[j])) { an[0] = one[i]; an[1] = 1; an[2] = one[j]; go(); } } } puts("-1"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> e[100006]; int n; bool nei(int a, int b) { for (int i = 0; i < 4; i++) { if (e[a][i] == b) return 1; } return 0; } bool is(int ans[], int x, int y) { bool v[100006] = {0}; int i, j; ans[0] = 1; v[1] = 1; ans[1] = e[1][x]; v[ans[1]] = 1; ans[2] = e[1][y]; v[ans[2]] = 1; int flag = 0; for (i = 1; i <= n - 3; i++) { flag = 0; for (j = 0; j < 4; j++) { if (!v[e[ans[i]][j]] && nei(ans[i + 1], e[ans[i]][j])) { v[e[ans[i]][j]] = 1; ans[i + 2] = e[ans[i]][j]; flag = 1; break; } } if (!flag) return 0; } return 1; } int main() { int i, j; scanf("%d", &n); for (i = 0; i < 2 * n; i++) { int a, b; scanf("%d%d", &a, &b); if (a == b) { puts("-1"); return 0; } e[a].push_back(b), e[b].push_back(a); } for (i = 1; i <= n; i++) { if (e[i].size() != 4) { puts("-1"); return 0; } } int flag = 0, ans[n + 1]; for (i = 0; i < 4; i++) { for (j = i + 1; j < 4; j++) { flag = 0; if (nei(e[1][i], e[1][j])) { flag = is(ans, i, j); if (!flag) flag = is(ans, j, i); if (flag) break; } } if (flag) break; } if (!flag) { puts("-1"); return 0; } for (i = 0; i < n; i++) { printf("%d ", ans[i]); } puts(""); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> v[100010], ans; int in_d[100010], flag[100010], col; bool no_flag; int main() { int n, i, m, a, b; scanf("%d", &n); m = 2 * n; for (i = 1; i <= m; i++) { scanf("%d %d", &a, &b); in_d[a]++; in_d[b]++; v[a].push_back(b); v[b].push_back(a); } for (i = 1; i <= n; i++) if (in_d[i] != 4) { no_flag = 1; break; } if (no_flag) { cout << "-1\n"; return 0; } int k, l, a1, b1, j; for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) { if (i != j) { no_flag = 0; a = v[1][i]; b = v[1][j]; ans.clear(); ans.push_back(1); ans.push_back(a); ans.push_back(b); col++; flag[1] = flag[a] = flag[b] = col; while (1) { no_flag = 0; for (k = 0; k < 4; k++) for (l = 0; l < 4; l++) if (v[a][k] == v[b][l] && flag[v[b][l]] != col) { swap(a, b); flag[v[b][k]] = col; b = v[b][k]; ans.push_back(b); k = l = 5; no_flag = 1; } if (no_flag != 1) break; } if (ans.size() == n) { no_flag = 0; a = ans[n - 1]; for (k = 0; k < 4; k++) if (v[a][k] == 1) { no_flag = 1; break; } if (no_flag) { no_flag = 0; a = ans[n - 2]; for (k = 0; k < 4; k++) if (v[a][k] == 1) { no_flag = 1; break; } } if (no_flag) { for (i = 0; i < n; i++) cout << ans[i] << " "; return 0; } } } } cout << "-1"; return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<vector<int> > G(n); set<pair<int, int> > kraw; for (int i = 0; i < 2 * n; ++i) { int a, b; scanf("%d %d", &a, &b); --a; --b; G[a].push_back(b); G[b].push_back(a); if (a > b) swap(a, b); kraw.insert(make_pair(a, b)); } for (int i = 0; i < n; ++i) { if (G[i].size() != 4) { printf("-1"); return 0; } } for (int l = 0; l < 4; ++l) { for (int p = 0; p < 4; ++p) { if (l != p) { int L = G[0][l]; int P = G[0][p]; vector<int> odp(n, -1); set<int> ust; odp[0] = 0; odp[1] = P; odp[n - 1] = L; ust.insert(0); ust.insert(P); ust.insert(L); for (int k = 0; k < 4; ++k) { if (G[P][k] != 0 && G[P][k] != L) { int K = G[P][k]; odp[2] = K; ust.insert(K); for (int i = 3; i <= n - 2; ++i) { if (odp[i - 2] == -1) continue; for (int j = 0; j < 4; ++j) { if (ust.find(G[odp[i - 2]][j]) == ust.end()) { odp[i] = G[odp[i - 2]][j]; ust.insert(G[odp[i - 2]][j]); break; } } } bool ok = true; if (ust.size() != n) ok = false; for (int i = 0; i < n; ++i) if (odp[i] == -1) ok = false; for (int i = 0; i < n; ++i) { if (odp[i] == -1) continue; int n1 = (i + 1) % n; int n2 = (n1 + 1) % n; int n3 = i - 1; if (n3 == -1) n3 = n - 1; int n4 = n3 - 1; if (n4 == -1) n4 = n - 1; if (odp[n1] != -1 && odp[n1] > odp[i]) if (kraw.find(make_pair(odp[i], odp[n1])) == kraw.end()) ok = false; if (odp[n2] != -1 && odp[n2] > odp[i]) if (kraw.find(make_pair(odp[i], odp[n2])) == kraw.end()) ok = false; if (odp[n3] != -1 && odp[n3] > odp[i]) if (kraw.find(make_pair(odp[i], odp[n3])) == kraw.end()) ok = false; if (odp[n4] != -1 && odp[n4] > odp[i]) if (kraw.find(make_pair(odp[i], odp[n4])) == kraw.end()) ok = false; } if (ok) { for (int i = 0; i < n; ++i) printf("%d ", odp[i] + 1); return 0; } for (int i = 2; i <= n - 2; ++i) { if (odp[i] == -1) continue; ust.erase(odp[i]); odp[i] = -1; } } } } } } printf("-1"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> v[1 << 20]; int n, a, b, sol[1 << 20]; map<pair<int, int>, bool> hashy; bool vi[1 << 20], is[10][10]; void dfs(int x, int pos) { if (sol[pos] != -1) return; vi[x] = 1; sol[pos] = x; int sz = v[x].size(), nxt; for (int j = 0; j < sz; j++) { nxt = v[x][j]; if (vi[nxt]) continue; int same = 0; for (int i = 0; i < 4; i++) { for (int k = 0; k < 4; k++) { if (v[x][i] == nxt || v[nxt][k] == x) continue; if (v[x][i] == v[nxt][k]) same++; } } if (same == 2) { dfs(nxt, pos + 1); continue; } } } int main() { memset(sol, -1, sizeof(sol)); memset(vi, 0, sizeof(vi)); memset(is, 0, sizeof(is)); cin >> n; for (int j = 1; j <= 2 * n; j++) { cin >> a >> b; if (hashy[make_pair(a, b)] || hashy[make_pair(b, a)]) continue; v[a].push_back(b); v[b].push_back(a); hashy[make_pair(a, b)] = 1; } for (int j = 1; j <= n; j++) { if (v[j].size() != 4) { puts("-1"); return 0; } } if (n == 5) { cout << "1 2 3 4 5" << endl; return 0; } if (n == 6) { int arr[6] = {1, 2, 3, 4, 5, 6}; for (int j = 1; j <= 6; j++) for (int i = 0; i < v[j].size(); i++) is[j][v[j][i]] = 1; do { bool issol = 1; for (int j = 0; j < 6; j++) { issol &= is[arr[j]][arr[(j + 1) % n]]; issol &= is[arr[j]][arr[(j + 2) % n]]; issol &= is[arr[j]][arr[(j - 1 + n) % n]]; issol &= is[arr[j]][arr[(j - 2 + n) % n]]; } if (issol) { for (int j = 0; j < 6; j++) cout << arr[j] << ' '; return 0; } } while (next_permutation(arr, arr + 6)); } dfs(1, 1); bool nosol = 0; for (int j = 1; j <= n; j++) { if (!vi[j]) nosol = 1; } if (nosol) { puts("-1"); } else for (int j = 1; j <= n; j++) cout << sol[j] << ' '; cout << endl; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const int max_n = 100005; int n; vector<int> a[max_n]; int res[max_n]; bool visited[max_n]; int same(int x, int y) { int r = 0; map<int, int> m; for (int i = 0; i < 4; i++) { m[a[x][i]]++; } for (int i = 0; i < 4; i++) { if (m[a[y][i]] == 1) r++; } return r; } int main() { scanf("%d", &n); int x, y; for (int i = 0; i < 2 * n; i++) { scanf("%d%d", &x, &y); a[x].push_back(y); a[y].push_back(x); } bool isres = 0; for (int i = 1; i <= n; i++) if (a[i].size() != 4) { printf("-1"); return 0; } if (n <= 5) { for (int i = 1; i <= n; i++) printf("%d ", i); } else if (n == 6) { int notin[7]; int perm[6] = {1, 2, 3, 4, 5, 6}; for (int i = 0; i < 720; i++) { bool b = 1; for (int j = 0; j < 6; j++) { notin[j] = perm[((j + 3) % 6)]; for (int k = 0; k < 4; k++) if (a[perm[j]][k] == notin[j]) b = 0; } if (b) { for (int i = 0; i < 6; i++) { printf("%d ", perm[i]); } return 0; } next_permutation(perm, perm + 6); } printf("-1"); } else { int nv = 0, prev = 0; int cv = 1; for (nv = 0; nv < n; nv++) { res[nv] = cv; visited[cv] = 1; isres = 0; for (int i = 0; i < 4; i++) { if ((!visited[a[cv][i]] or (nv == n - 1 and a[cv][i] == 1)) and a[cv][i] != prev and same(cv, a[cv][i]) == 2) { prev = cv; cv = a[cv][i]; isres = 1; break; } } if (!isres) break; } if (isres) { for (int i = 0; i < n; i++) printf("%d ", res[i]); } else printf("-1"); } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int adj[200011][30], deg[200011], V[200011], ans[200011], ok, cnt = 3; map<pair<int, int>, int> vst; void dfs(int u, int fa1) { int vv = 0; for (int i = 0; i < 4; i++) { int v = adj[u][i]; if (V[v]) continue; if (vst[pair<int, int>(v, fa1)]) { V[v] = 1; ans[cnt++] = v; dfs(v, u); } } } int main() { int n, m, i, j; cin >> n; for (i = 0; i < 2 * n; i++) { int u, v; scanf("%d%d", &u, &v); adj[u][deg[u]++] = v; adj[v][deg[v]++] = u; if (deg[u] > 4 || deg[v] > 4) { puts("-1"); return 0; } vst[pair<int, int>(u, v)] = 1; vst[pair<int, int>(v, u)] = 1; } int uu, vv, flag = 0; for (i = 1; i <= n; i++) adj[i][4] = i; for (i = 0; i < 4; i++) { int cnt = 0; int u = adj[1][i]; for (j = 0; j < 5; j++) { for (int p = 0; p < 5; p++) { if (adj[1][p] == adj[u][j]) cnt++; } } if (cnt >= 4 && flag) { uu = u; break; } if (cnt >= 4 && flag == 0) vv = u, flag = 1; } ans[0] = vv, ans[1] = 1, ans[2] = uu; V[1] = V[vv] = V[uu] = 1; dfs(uu, 1); if (cnt != n) puts("-1"); else for (i = 0; i < n; i++) printf("%d ", ans[i]); puts(""); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int n; vector<int> g[100005]; int nb[100005][2]; bool visited[100005]; void solve6() { int p[7], i, j; memset(p, 0, sizeof(p)); for (i = 1; i <= 6; ++i) { if (g[i].size() != 4) { printf("-1\n"); return; } sort(g[i].begin(), g[i].end()); for (j = 1; j <= 6; ++j) { if (j == i) continue; if (find(g[i].begin(), g[i].end(), j) == g[i].end()) { p[i] = j; break; } } } for (i = 1; i <= 6; ++i) { int k = p[i]; if (p[k] != i) { printf("-1\n"); return; } } vector<bool> visited(7, false); vector<int> left, right; for (i = 1; i <= 6; ++i) { if (!visited[i]) { visited[i] = true; left.push_back(i); right.push_back(p[i]); visited[p[i]] = true; } } left.insert(left.end(), right.begin(), right.end()); for (i = 0; i < 6; ++i) printf("%d%c", left[i], i == 5 ? '\n' : ' '); } bool check(int u, int v) { return find(g[u].begin(), g[u].end(), v) != g[u].end(); } int main() { int i, a, b; scanf("%d", &n); for (i = 0; i < 2 * n; ++i) { scanf("%d %d", &a, &b); g[a].push_back(b); g[b].push_back(a); } if (n == 5) { printf("1 2 3 4 5\n"); return 0; } if (n == 6) { solve6(); return 0; } for (i = 1; i <= n; ++i) { if (g[i].size() != 4) { printf("-1\n"); return 0; } int j, k; bool connected[4][4]; memset(connected, false, sizeof(connected)); int cnt[4]; memset(cnt, 0, sizeof(cnt)); for (j = 0; j < 3; ++j) { for (k = j + 1; k < 4; ++k) { connected[j][k] = check(g[i][j], g[i][k]); if (connected[j][k]) { cnt[j]++; cnt[k]++; } } } if (accumulate(cnt, cnt + 4, 0) != 6) { printf("-1\n"); return 0; } int c2[4], c1[4], idx[2] = {0, 0}; for (j = 0; j < 4; ++j) { if (cnt[j] == 2) c2[idx[1]++] = j; else if (cnt[j] == 1) c1[idx[0]++] = j; } if (idx[0] != 2 || idx[1] != 2) { printf("-1\n"); return 0; } nb[i][0] = g[i][c2[0]]; nb[i][1] = g[i][c2[1]]; } memset(visited, false, sizeof(visited)); vector<int> path(n); path[0] = 1; int p = nb[1][0]; path[1] = nb[1][0]; visited[1] = true; visited[nb[1][0]] = true; for (i = 2; i < n; ++i) { bool found = false; for (int j = 0; j < 2; ++j) { if (!visited[nb[p][j]]) { int k = nb[p][j]; visited[k] = true; path[i] = k; found = true; p = k; break; } } if (!found) { printf("-1\n"); return 0; } } for (i = 0; i < n; ++i) printf("%d%c", path[i], i == n - 1 ? '\n' : ' '); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> v[100001]; int check(int p, int q) { int k = 0, u = 0, t = 0; while (u < 4 && t < 4) { if (v[p][u] == v[q][t]) k++, u++, t++; else if (v[p][u] < v[q][t]) u++; else t++; } return k; } int main() { int n, a, b; cin >> n; for (int i = 0; i < 2 * n; i++) cin >> a >> b, v[a].push_back(b), v[b].push_back(a); for (int i = 1; i <= n; i++) if (v[i].size() != 4) { cout << "-1"; return 0; } if (n == 5) cout << "1 2 3 4 5"; else { for (int i = 1; i <= n; i++) sort(v[i].begin(), v[i].end()); if (n == 6) { set<pair<int, int> > s; for (int i = 1; i <= 6; i++) for (int j = i + 1; j <= 6; j++) if (check(i, j) == 4) s.insert(make_pair(i, j)); for (set<pair<int, int> >::iterator ii = s.begin(); ii != s.end(); ii++) cout << (*ii).first << " "; for (set<pair<int, int> >::iterator ii = s.begin(); ii != s.end(); ii++) cout << (*ii).second << " "; } else { int k = 1; for (int i = 1; i <= n; i++) for (int j = 0, flag = 1; j < 4 && flag; j++) if (check(k, v[k][j]) == 2 && v[v[k][j]].size() == 4) v[k].push_back(v[k][j]), k = v[k][j], flag = 0; k = 0; for (int i = 1; i <= n && k <= 1; i++) if (v[i].size() == 4) k++; if (k != 1) cout << "-1"; else { cout << "1", k = 1; for (int i = 1; i < n; i++) k = v[k][4], cout << " " << k; } } } }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int n; vector<int> a[100000 + 5], v; bool vis[100000 + 5]; void DFS(int u) { if (v.size() == n) { for (int i = 0; i < v.size(); i++) printf("%d ", v[i]); exit(0); } for (int i = 0; i < a[u].size(); i++) { int k = a[u][i]; if (vis[k]) continue; if (v.size() >= 2) if (!binary_search(a[k].begin(), a[k].end(), v[v.size() - 2])) continue; vis[k] = true; v.push_back(k); DFS(k); v.pop_back(); vis[k] = false; } } int main() { scanf(" %d ", &n); for (int i = 0; i < 2 * n; i++) { int u, v; scanf(" %d %d ", &u, &v); a[u].push_back(v); a[v].push_back(u); } for (int i = 1; i <= n; i++) { if (a[i].size() != 4) { puts("-1"); return 0; } sort(a[i].begin(), a[i].end()); } vis[1] = true; v.push_back(1); DFS(1); puts("-1"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 100017; int n, deg[maxn], arr[maxn], res[maxn]; vector<int> a[maxn]; bool free1[maxn]; bool check() { int c, v; memset(free1, 1, sizeof(free1)); for (int i = 1, _c = 3; i <= _c; i++) free1[arr[i]] = 0; arr[n + 1] = arr[1]; arr[n + 2] = arr[2]; arr[n + 3] = arr[3]; arr[n + 4] = arr[4]; for (int i = 3, _c = n - 2; i <= _c; i++) { c = 0; for (int j = 0, _a = (int(a[arr[i]].size())); j < _a; ++j) { v = a[arr[i]][j]; if (v == arr[i - 1]) c++; else if (v == arr[i - 2]) c++; else if (v == arr[i + 1]) c++; else { arr[i + 2] = v; if (!free1[v]) return 0; free1[v] = 0; } } if (c != 3) return 0; } for (int i = n - 1, _c = n + 2; i <= _c; i++) { c = 0; for (int j = 0, _a = (int(a[arr[i]].size())); j < _a; ++j) { v = a[arr[i]][j]; if (v == arr[i - 1]) c++; else if (v == arr[i - 2]) c++; else if (v == arr[i + 1]) c++; else if (v == arr[i + 2]) c++; } if (c != 4) return 0; } return 1; } int main() { scanf("%d", &n); memset(deg, 0, sizeof(deg)); int u, v; for (int i = 1, _c = n * 2; i <= _c; i++) { scanf("%d%d", &u, &v); deg[u]++; deg[v]++; if (deg[u] > 4 || deg[v] > 4) { puts("-1"); return 0; } a[u].push_back(v); a[v].push_back(u); } arr[1] = 1; bool ok = 0; for (int i = 0, _a = (int(a[1].size())); i < _a; ++i) { for (int j = 0, _a = (int(a[1].size())); j < _a; ++j) if (i != j) { arr[2] = a[1][i]; arr[3] = a[1][j]; u = arr[3]; for (int z = 0, _a = (int(a[u].size())); z < _a; ++z) { v = a[u][z]; if (v != arr[1] && v != arr[2]) { arr[4] = v; if (check()) { ok = 1; for (int i = 1, _c = n; i <= _c; i++) res[i] = arr[i]; break; } } } if (ok) break; } if (ok) break; } if (!ok) { puts("-1"); } else { for (int i = 1, _c = n; i <= _c; i++) printf("%d ", res[i]); puts(""); } return 0; }
CPP