QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#784283#9784. Donkey and Puss in Bootsucup-team4975#WA 0ms3604kbC++232.5kb2024-11-26 14:23:542024-11-26 14:23:58

Judging History

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

  • [2024-11-26 14:23:58]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3604kb
  • [2024-11-26 14:23:54]
  • 提交

answer

#define LOCAL
#include <bits/stdc++.h>
#define fir first
#define sec second
#define el '\n'

#ifdef LOCAL
#define FINISH cerr << "FINISH" << endl;
#else
#define FINISH ;
#endif

#ifdef LOCAL
#define debug(x) cerr << setw(4) << #x << " == " << x << endl
#else
#define debug(x)
#endif

#ifdef LOCAL
#define debugv(x)                   \
    cerr << setw(4) << #x << ":: "; \
    for (auto i : x)                \
        cerr << i << " ";           \
    cerr << endl
#else
#define debugv(x)
#endif

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
ostream& operator<<(ostream& out, PII& x)
{
    out << x.fir << " " << x.sec << endl;
    return out;
}

const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int N = 200020;
void solve()
{
    int n;
    cin >> n;
    vector<vector<int>> edge(n + 1);
    vector<int> inp(n + 1, 0);
    for (int i = 1; i < n; i++) {
    	int x, y;
    	cin >> x >> y;
    	edge[x].push_back(y);
    	edge[y].push_back(x);
    	inp[x]++, inp[y]++;
    }
    vector<int> f(n + 1, 0), dep(n + 1, 0);
    auto dfs = [&] (auto &self, int x, int fa)-> void {
    	f[x] = fa;
    	dep[x] = dep[fa] + 1;
    	for (auto to : edge[x]) {
    		if (to == fa)
    			continue;
    		self(self, to, x);
    		
    	}
    };

    int rt = 0, sum = inf;
    for (int i = 1; i <= n; i++) {
    	for (int j = 1; j <= n; j++) {
    		dep[j] = f[j] = 0;
    	}

    	dfs(dfs, i, 0);
    	int now = 0;
    	for (int j = 1; j <= n; j++) {
    		if (dep[j] >= 3) {
    			now += dep[j] - 2;
    		}
    	}

    	if (now < sum) {
    		sum = now;
    		rt = i;
    	}
    }
	// cout << rt <<el;
    dfs(dfs, rt, 0);
    vector<array<int, 3>> ans;
    vector<int> res;
    vector<int> v;
    for (int i = 1; i <= n; i++) {
    	if (inp[i] == 1) {
    		res.push_back(i);
    	}
    }
    int flag = 1;
    while (flag) {
    	flag = 0;
    	for (int i = 0; i < res.size(); i++) {
    		int z = res[i], y = f[res[i]], x = f[f[res[i]]];
    		if (x == 0)
    			continue;
    		flag = 1;
    		ans.push_back({x, y, z});
    		f[z] = x;
    		res.push_back(y);
    	}

    }
    cout << ans.size() <<el;
    for (auto [x, y, z] : ans) {
    	cout << x << " " << y <<" " << z << el;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T = 1;
    // cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3604kb

input:

2
2 2

output:

0

result:

wrong answer 1st lines differ - expected: 'Puss in Boots', found: '0'