QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#111237#6307. Chase Game 2xaphoenix#WA 16ms6388kbC++142.1kb2023-06-06 14:11:432023-06-06 14:11:44

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-06 14:11:44]
  • 评测
  • 测评结果:WA
  • 用时:16ms
  • 内存:6388kb
  • [2023-06-06 14:11:43]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define LC k<<1
#define RC k<<1|1
#define IO cin.sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define rep(i,a,n) for (int i = a; i < n; i++)
#define repn(i,a,n) for (int i = a; i <= n; i++)
#define per(i,a,n) for (int i = (n) - 1; i >= a; i--)
#define pern(i,a,n) for (int i = n; i >= a; i--)

typedef long long LL;
typedef long double LD;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<double, double> PDD;
typedef pair<ull, ull> PUU;
typedef pair<LL, LL> PLL;

const int N = 110000;
const int M = 1100000;
const int mod = 1e9+7;
const int inf = (int)1e9;
const LL INF = 1e18;
const double eps = 1e-9;

mt19937_64 Rand((unsigned long long)new char);
#define rand Rand

int T, n, dp[N], rt, du[N], mx, ans;
vector<int> g[N];
void dfs(int x, int fa) {
	if (du[x] == 1) {
		dp[x] = 1;
		return;
	}
	dp[x] = 0;
	int num = 0;
	set<PII> s;
	for (auto y: g[x]) {
		if (y != fa) {
			dfs(y, x);
			if (du[y] == 0) num += 1;
			else if (dp[y]) s.insert(mp(dp[y], y));
		}
	}
	if (num) s.insert(mp(num, x));
	while (s.size() > 1) {
		auto it = s.begin();
		auto it2 = s.rbegin();
		PII a = (*it), b = (*it2);
		s.erase(a), s.erase(b);
		if (a.fi > 1) s.insert(mp(a.fi - 1, a.se));
		if (b.fi > 1) s.insert(mp(b.fi - 1, b.se));
		ans += 1;
	}
	if (s.size() > 0) {
		dp[x] = s.begin() -> fi;
	}
}
int main() {
	IO;
	cin >> T;
	while (T--) {
		cin >> n;
		repn(i, 1, n) g[i].clear(), du[i] = 0;
		rep(i, 1, n) {
			int x, y;
			cin >> x >> y;
			g[x].pb(y), g[y].pb(x);
			du[x]++, du[y]++;
		}
		if (n <= 3) {
			cout << "-1\n";
			continue;
		}
		mx = 0;
		repn(i, 1, n) {
			if (du[i] > 1) rt = i;
			mx = max(mx, du[i]);
		}
		if (mx == n - 1) {
			cout << "-1\n";
			continue;
		}
		ans = 0;
		dfs(rt, 0);
		cout << ans + dp[rt] << "\n";
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 6388kb

input:

4
2
1 2
4
1 2
2 3
3 4
4
1 2
2 3
2 4
5
1 2
2 3
3 4
3 5

output:

-1
1
-1
2

result:

ok 4 number(s): "-1 1 -1 2"

Test #2:

score: 0
Accepted
time: 8ms
memory: 6016kb

input:

10000
4
1 2
1 3
3 4
4
1 2
1 3
1 4
4
1 2
2 3
1 4
5
1 2
2 3
1 4
4 5
5
1 2
2 3
3 4
4 5
4
1 2
2 3
2 4
5
1 2
1 3
2 4
2 5
4
1 2
2 3
1 4
5
1 2
1 3
2 4
1 5
5
1 2
2 3
3 4
2 5
5
1 2
1 3
2 4
2 5
4
1 2
1 3
3 4
5
1 2
1 3
3 4
1 5
4
1 2
1 3
1 4
5
1 2
1 3
3 4
3 5
5
1 2
2 3
3 4
3 5
4
1 2
1 3
2 4
5
1 2
2 3
2 4
3 5
5
...

output:

1
-1
1
1
1
-1
2
1
2
2
2
1
2
-1
2
2
1
2
2
1
1
1
-1
2
2
2
1
-1
1
1
2
1
1
-1
1
2
1
1
1
-1
1
1
2
2
2
1
1
1
-1
1
2
1
1
2
1
2
1
1
2
-1
-1
-1
2
2
2
1
1
1
2
2
2
-1
1
2
-1
1
1
-1
2
-1
-1
1
2
2
2
1
1
1
1
1
1
1
1
1
2
-1
1
1
2
-1
2
1
1
1
-1
2
-1
1
-1
-1
2
-1
2
1
2
2
1
1
1
1
2
1
1
1
1
-1
2
1
2
1
1
1
1
1
1
1
2
-1...

result:

ok 10000 numbers

Test #3:

score: -100
Wrong Answer
time: 16ms
memory: 6312kb

input:

10000
9
1 2
2 3
3 4
4 5
1 6
6 7
5 8
7 9
9
1 2
2 3
2 4
1 5
2 6
4 7
6 8
1 9
9
1 2
2 3
1 4
4 5
5 6
4 7
2 8
1 9
10
1 2
2 3
1 4
3 5
3 6
2 7
6 8
6 9
6 10
10
1 2
1 3
3 4
2 5
1 6
5 7
4 8
2 9
7 10
10
1 2
2 3
2 4
1 5
3 6
6 7
5 8
4 9
9 10
9
1 2
2 3
2 4
1 5
3 6
2 7
1 8
2 9
9
1 2
1 3
2 4
1 5
3 6
3 7
7 8
8 9
10
1...

output:

1
3
3
3
2
2
3
2
3
2
3
2
3
2
3
3
3
2
3
2
3
2
3
3
2
2
3
3
3
3
3
3
2
3
3
3
3
3
3
3
2
3
3
3
3
3
2
3
3
3
3
3
3
2
3
2
3
2
2
3
3
4
3
3
3
3
2
2
3
2
2
2
3
3
2
3
3
2
3
3
3
3
2
3
2
2
3
3
3
3
2
3
3
2
3
3
3
3
3
2
3
2
2
3
3
3
3
2
2
3
2
3
4
2
4
3
2
3
3
2
3
2
3
3
3
3
2
2
3
3
3
2
3
3
3
3
3
3
2
3
3
2
2
4
3
3
3
3
2
3
...

result:

wrong answer 37th numbers differ - expected: '4', found: '3'