QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#601502 | #5064. DFS Order | gozonite# | RE | 0ms | 0kb | C++14 | 1.3kb | 2024-09-30 02:14:01 | 2024-09-30 02:14:01 |
answer
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <cstdio>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <deque>
#include <queue>
#include <tuple>
#include <cmath>
#include <cctype>
#include <stack>
#include <cassert>
#include <iomanip>
#include <random>
#include <chrono>
using namespace std;
using ll = long long;
int n;
vector<int> adj[int(1e5 + 1)];
int depth[100001], subs[100001];
void dfs(int v, int p, int d) {
depth[v] = d;
subs[v] = 1;
for (auto u : adj[v]) {
if (u == p) continue;
dfs(u, v, d+1);
subs[v] += subs[u];
}
}
int main() {
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
int T; cin >> T;
while (T--) {
// cout << "processing: " << T << endl;
cin >> n;
for (int i = 0; i < n-1; i++) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, -1, 0);
for (int i = 1; i <= n; i++) {
cout << depth[i]+1 << " " << n-subs[i]+1 << endl;
}
for (int i = 1; i <= n; i++) adj[i].clear();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
2 4 1 2 2 3 3 4 5 1 2 2 3 2 4 1 5