답안 #441209

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
441209 2021-07-04T16:11:52 Z sinamhdv Pipes (CEOI15_pipes) C++11
80 / 100
5000 ms 14404 KB
// CEOI15_pipes
// 1st solution (reading input twice)
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;

#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'

#define MAXN 100010
#define LOGN 19

int n, m;
bitset<60 * MAXN> intree;
vector<int> adj[MAXN];
int lpar[LOGN][MAXN], h[MAXN];
bitset<MAXN> mark;
int val[MAXN];

int dpar[MAXN];
int getroot(int x)
{
	return dpar[x] == x ? x : dpar[x] = getroot(dpar[x]);
}
bool merge(int x, int y)
{
	x = getroot(x);
	y = getroot(y);
	if (x == y) return false;
	dpar[x] = y;
	return true;
}

void dfs1(int v)
{
	mark[v] = true;
	for (int u : adj[v]) if (!mark[u])
	{
		lpar[0][u] = v;
		h[u] = h[v] + 1;
		dfs1(u);
	}
}

void dfs2(int v)
{
	mark[v] = true;
	for (int u : adj[v]) if (!mark[u]) dfs2(u), val[v] += val[u];
	if (lpar[0][v] && val[v] == 0) printf("%d %d\n", v, lpar[0][v]);
}

int getlca(int x, int y)
{
	if (h[y] < h[x]) swap(x, y);
	FOR(i, 0, LOGN) if ((h[y] - h[x]) >> i & 1) y = lpar[i][y];
	if (x == y) return x;
	for (int i = LOGN - 1; i >= 0; i--) if (lpar[i][x] != lpar[i][y])
	{
		x = lpar[i][x];
		y = lpar[i][y];
	}
	return lpar[0][x];
}

int32_t main(void)
{
	scanf("%d %d", &n, &m);

	iota(dpar, dpar + n + 5, 0);

	FOR(i, 0, m)
	{
		int x, y;
		scanf("%d %d", &x, &y);
		if (merge(x, y))
		{
			intree[i] = 1;
			adj[x].pb(y);
			adj[y].pb(x);
		}
	}

	FOR(i, 1, n + 1) if (!mark[i]) dfs1(i);

	FOR(i, 1, LOGN) FOR(j, 1, n + 1) lpar[i][j] = lpar[i - 1][lpar[i - 1][j]];

	rewind(stdin);
	scanf("%d %d", &n, &m);
	FOR(i, 0, m)
	{
		int x, y;
		scanf("%d %d", &x, &y);
		if (intree[i]) continue;
		val[x]++;
		val[y]++;
		val[getlca(x, y)] -= 2;
	}

	mark.reset();
	FOR(i, 1, n + 1) if (!mark[i]) dfs2(i);

	return 0;
}

Compilation message

pipes.cpp: In function 'int32_t main()':
pipes.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |  scanf("%d %d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:93:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:107:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |  scanf("%d %d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:111:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2764 KB Output is correct
2 Correct 2 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 3276 KB Output is correct
2 Correct 9 ms 3276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 359 ms 3144 KB Output is correct
2 Correct 345 ms 3152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 683 ms 3872 KB Output is correct
2 Correct 767 ms 3908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1130 ms 5524 KB Output is correct
2 Correct 927 ms 6468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1666 ms 10920 KB Output is correct
2 Correct 1368 ms 10972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2645 ms 12196 KB Output is correct
2 Correct 2424 ms 12420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4497 ms 14272 KB Output is correct
2 Correct 3876 ms 14404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5096 ms 14276 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5094 ms 13716 KB Time limit exceeded
2 Halted 0 ms 0 KB -