답안 #527125

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
527125 2022-02-17T02:24:54 Z maomao90 Potemkin cycle (CEOI15_indcyc) C++17
0 / 100
351 ms 5616 KB
#include <bits/stdc++.h>
using namespace std;

#define REP(i, j, k) for (int i = j; i < k; i++)
#define RREP(i, j, k) for (int i = j; i >= k; i--)

template<class T>
inline bool mxto(T &a, T b) {return a < b ? a = b, 1 : 0;}
template<class T>
inline bool mnto(T &a, T b) {return a > b ? a = b, 1 : 0;}

typedef long long ll;
#define FI first
#define SE second
#define MP make_pair
typedef pair<int, int> ii;
#define pb push_back
#define ALL(x) x.begin(), x.end()
typedef vector<int> vi;
typedef vector<ii> vii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

#define MAXN 1005
#define INF 1000000005
#define LINF 1000000000000000005ll

int n, r;
vi adj[MAXN], nadj[MAXN];
int grid[MAXN][MAXN];
vii eg;
bool vis[MAXN];
int p[MAXN], dist[MAXN];

int uf[MAXN], rnk[MAXN];
int findp(int i) {
	if (uf[i] == i) return i;
	return uf[i] = findp(uf[i]);
}
bool join(int a, int b) {
	int pa = findp(a), pb = findp(b);
	if (pa == pb) return 0;
	if (rnk[pa] < rnk[pb]) swap(pa, pb);
	if (rnk[pa] == rnk[pb]) rnk[pa]++;
	uf[pb] = pa;
	return 1;
}

int main() {
#ifndef DEBUG
	ios::sync_with_stdio(0), cin.tie(0);
#endif
	cin >> n >> r;
	REP (i, 0, n + 1) {
		uf[i] = i;
	}
	REP (i, 0, r) {
		int a, b; cin >> a >> b;
		adj[a].pb(b);
		adj[b].pb(a);
		grid[a][b] = grid[b][a] = 1;
		eg.pb(MP(a, b));
	}
	for (auto [a, b] : eg) {
		REP (i, 1, n + 1) {
			if (grid[i][a] == 1 && grid[i][b] == 1) {
				join(i, a);
				join(i, b);
			}
		}
	}
	for (auto [a, b] : eg) {
		int pa = findp(a), pb = findp(b);
		if (pa == pb) continue;
		cerr << pa << " <-> " << pb << '\n';
		nadj[pa].pb(pb);
		nadj[pb].pb(pa);
	}
	REP (i, 1, n + 1) {
		if (findp(i) != i) continue;
		if (vis[i]) continue;
		queue<int> bfs;
		vis[i] = 1;
		bfs.push(i);
		cerr << i << '\n';
		while (!bfs.empty()) {
			int u = bfs.front(); bfs.pop();
			cerr << ' ' << u << '\n';
			for (int v : nadj[u]) {
				if (p[u] == v) continue;
				cerr << "  " << v << '\n';
				if (vis[v]) {
					// dist[v] <= dist[u]
					vi tans;
					while (dist[u] > dist[v]) {
						tans.pb(u);
						u = p[u];
					}
					assert(dist[u] == dist[v]);
					vi rans;
					while (u != v) {
						tans.pb(u);
						rans.pb(v);
						u = p[u];
						v = p[v];
					}
					tans.pb(u);
					reverse(ALL(rans));
					for (int a : rans) {
						tans.pb(a);
					}
					vi ans;
					REP (i, 0, tans.size()) {
						ans.pb(tans[i]);
						int u = tans[i], 
							v = tans[i + 1 == tans.size() ? 0 : i + 1];
						if (grid[u][v] != 1) {
							bool found = 0;
							REP (j, 1, n + 1) {
								if (grid[u][j] == 1 && grid[j][v] == 1) {
									found = 1;
									ans.pb(j);
									break;
								}
							}
							assert(found);
						}
					}
					for (int i : ans) {
						cout << i << ' ';
					}
					cout << '\n';
					return 0;
				}
				vis[v] = 1;
				p[v] = u;
				dist[v] = dist[u] + 1;
				bfs.push(v);
			}
		}
	}
	cout << "no\n";
	return 0;
}

Compilation message

indcyc.cpp: In function 'int main()':
indcyc.cpp:4:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define REP(i, j, k) for (int i = j; i < k; i++)
......
  115 |      REP (i, 0, tans.size()) {
      |           ~~~~~~~~~~~~~~~~~             
indcyc.cpp:115:6: note: in expansion of macro 'REP'
  115 |      REP (i, 0, tans.size()) {
      |      ^~~
indcyc.cpp:118:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |        v = tans[i + 1 == tans.size() ? 0 : i + 1];
      |                 ~~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 592 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 1356 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 716 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1612 KB Output is correct
2 Incorrect 3 ms 1580 KB Expected integer, but "no" found
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 1612 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 147 ms 5616 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 66 ms 4880 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 351 ms 4548 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -