icpc 2019 徐州區域賽
技術標籤:ACM--比賽補題
文章目錄
2019 ICPC Asia Xuzhou Regional
A. Cat
題意: 給出一個區間[ l , r ] 區間內的數字是l , l + 1 , l + 2 , . . . , r ,讓你找到一個最大的區間使得區間異或和小於s .
題解: 發現以偶數開頭的連續4個可以異或完為0,因此對於R來說,如果R是奇數,那麼R往前4的倍數;如果R是偶數,那麼R–,然後往前4的倍數。最後剩下的暴力列舉。
程式碼:
#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
LL check(LL l, LL r) {
if (l > r) return 0;
LL sum = 0;
if (r - l + 1 <= 10) {
for (LL i = l; i <= r; i++) sum ^= i;
return sum;
} else {
while (l % 4 != 0) sum ^= l, l++;
while (r % 4 != 3) sum ^= r, r--;
return sum;
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
LL l, r, s;
scanf("%lld%lld%lld", &l, &r, &s);
LL ans = -1;
for (int i = 0; i <= 3; i++) {
for (int j = 0; j <= 3; j++) {
LL L = l + i, R = r - j;
if (check(L, R) <= s) ans = max(ans, R - L + 1);
}
}
printf("%lld\n", ans == 0 ? -1 : ans);
}
return 0;
}
C. ❤️ numbers
題意:
題解: 打表題
程式碼:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int L, R;
bool prime(int x) {
for (int i = 2; 1ll * i * i <= x; ++i) {
if (x % i == 0)
return false;
}
return true;
}
int main() {
int T; scanf("%d", &T);
while (T--) {
scanf("%d%d", &L, &R);
if (R - L + 1 > 60) {
puts("Yes");
} else {
int tot = R - L + 1;
int p = 0;
for (int i = L; i <= R; ++i) {
if (prime(i)) ++p;
}
puts(p * 3 < tot ? "Yes" : "No");
}
}
return 0;
}
E. Multiply
題意: 給定 Z = a 1 ! ∗ a 2 ! ∗ . . . ∗ a n ! Z = a_1! * a_2! * ... * a_n! Z=a1!∗a2!∗...∗an!,給定關係 b i = Z ∗ X i b_i = Z * X^i bi=Z∗Xi和X、Y,要求找到最小的下標i,使得 b i ∣ Y ! b_i | Y! bi∣Y!
題解: 本題沒有告訴你無解列印啥,推測必然有解,那麼答案大於等0。那麼如果Z|Y,則輸出0;否則,將X做質因子分解,然後只需要計算上寫的階乘內這些質因子有多少次冪。
程式碼:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int const MAXN = 1e5 + 10;
int T;
LL n, x, y, a[MAXN];
const int S=20;//隨機演算法判定次數,S越大,判錯概率越小
//計算 (a*b)%c. a,b都是LL的數,直接相乘可能溢位的
// a,b,c <2^63
LL mult_mod(LL a,LL b,LL c) {
a%=c;
b%=c;
LL ret=0;
while(b) {
if(b&1){ret+=a;ret%=c;}
a<<=1;
if(a>=c)a%=c;
b>>=1;
}
return ret;
}
//計算 x^n %c
LL pow_mod(LL x,LL n,LL mod) {
if(n==1)return x%mod;
x%=mod;
LL tmp=x;
LL ret=1;
while(n) {
if(n&1) ret=mult_mod(ret,tmp,mod);
tmp=mult_mod(tmp,tmp,mod);
n>>=1;
}
return ret;
}
//以a為基,n-1=x*2^t a^(n-1)=1(mod n) 驗證n是不是合數
//一定是合數返回true,不一定返回false
bool check(LL a,LL n,LL x,LL t) {
LL ret=pow_mod(a,x,n);
LL last=ret;
for(int i=1;i<=t;i++) {
ret=mult_mod(ret,ret,n);
if(ret==1&&last!=1&&last!=n-1) return true;//合數
last=ret;
}
if(ret!=1) return true;
return false;
}
// Miller_Rabin()演算法素數判定
//是素數返回true.(可能是偽素數,但概率極小)
//合數返回false;
bool Miller_Rabin(LL n) {
if(n<2)return false;
if(n==2)return true;
if((n&1)==0) return false;//偶數
LL x=n-1;
LL t=0;
while((x&1)==0){x>>=1;t++;}
for(int i=0;i<S;i++) {
LL a=rand()%(n-1)+1;//rand()需要stdlib.h標頭檔案
if(check(a,n,x,t))
return false;//合數
}
return true;
}
//************************************************
//pollard_rho 演算法進行質因數分解
//************************************************
LL factor[MAXN];//質因數分解結果(剛返回時是無序的)
int tol;//質因數的個數。陣列小標從0開始
LL gcd(LL a,LL b) {
if(a==0)return 1;
if(a<0) return gcd(-a,b);
while(b) {
LL t=a%b;
a=b;
b=t;
}
return a;
}
LL Pollard_rho(LL x,LL c) {
LL i=1,k=2;
LL x0=rand()%x;
LL y=x0;
while(1) {
i++;
x0=(mult_mod(x0,x0,x)+c)%x;
LL d=gcd(y-x0,x);
if(d!=1&&d!=x) return d;
if(y==x0) return x;
if(i==k){y=x0;k+=k;}
}
}
//對n進行素因子分解
void findfac(LL n) {
if(Miller_Rabin(n)) {//素數
factor[tol++]=n;
return;
}
LL p=n;
while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1);
findfac(p);
findfac(n/p);
}
int main() {
cin >> T;
while(T--) {
unordered_map<LL, LL> mp;
scanf("%lld%lld%lld", &n, &x, &y);
for (int i = 1; i <= n; ++i) scanf("%lld", a + i);
tol=0;
findfac(x);
for(int i=0;i<tol;i++) mp[factor[i]]++;
LL ans = 1e18 + 10;
for (auto f: mp) {
LL prime = f.first, cnt = f.second, sum = 0;
for (int i = 1; i <= n; ++i) {
for (LL j = a[i]; j; j /= prime) sum -= j / prime;
}
for (LL j = y; j; j /= prime) sum += j / prime;
ans = min(ans, sum / cnt);
}
printf("%lld\n", ans);
}
return 0;
}
F. The Answer to the Ultimate Question of Life, The Universe, and Everything.
題意: 找到 a 3 + b 3 + c 3 = x a^3+b^3+c^3=x a3+b3+c3=x的a、b、c
題解: 現在本機把x的表打出來,預處理 a 3 + b 3 a^3+b^3 a3+b3和 c 3 c^3 c3,然後去列舉 a 3 + b 3 a^3+b^3 a3+b3,將 x − ( a 3 + b 3 ) x-(a^3 + b^3) x−(a3+b3)去 c 3 c^3 c3裡面二分查詢,判斷是否存在即可。
程式碼:
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int x;
cin >> x;
if (x == 0)
cout << -5000 << ' ' << 0 << ' ' << 5000 << endl;
else if (x == 1)
cout << -5000 << ' ' << 1 << ' ' << 5000 << endl;
else if (x == 8)
cout << -5000 << ' ' << 2 << ' ' << 5000 << endl;
else if (x == 27)
cout << -5000 << ' ' << 3 << ' ' << 5000 << endl;
else if (x == 64)
cout << -5000 << ' ' << 4 << ' ' << 5000 << endl;
else if (x == 125)
cout << -5000 << ' ' << 5 << ' ' << 5000 << endl;
else if (x == 183)
cout << -4889 << ' ' << 976 << ' ' << 4876 << endl;
else if (x == 181)
cout << -4874 << ' ' << 974 << ' ' << 4861 << endl;
else if (x == 190)
cout << -4812 << ' ' << -593 << ' ' << 4815 << endl;
else if (x == 111)
cout << -4793 << ' ' << -2312 << ' ' << 4966 << endl;
else if (x == 161)
cout << -4767 << ' ' << -2476 << ' ' << 4980 << endl;
else if (x == 2)
cout << -4373 << ' ' << -486 << ' ' << 4375 << endl;
else if (x == 118)
cout << -4328 << ' ' << 383 << ' ' << 4327 << endl;
else if (x == 87)
cout << -4126 << ' ' << -1972 << ' ' << 4271 << endl;
else if (x == 162)
cout << -4125 << ' ' << -1417 << ' ' << 4180 << endl;
else if (x == 16)
cout << -4114 << ' ' << -588 << ' ' << 4118 << endl;
else if (x == 92)
cout << -4052 << ' ' << 861 << ' ' << 4039 << endl;
else if (x == 127)
cout << -4034 << ' ' << -3881 << ' ' << 4988 << endl;
else if (x == 128)
cout << -3989 << ' ' << -726 << ' ' << 3997 << endl;
else if (x == 48)
cout << -3950 << ' ' << -1247 << ' ' << 3991 << endl;
else if (x == 54)
cout << -3885 << ' ' << -648 << ' ' << 3891 << endl;
else if (x == 160)
cout << -3874 << ' ' << -1654 << ' ' << 3972 << endl;
else if (x == 170)
cout << -3834 << ' ' << -2149 << ' ' << 4047 << endl;
else if (x == 198)
cout << -3752 << ' ' << -1347 << ' ' << 3809 << endl;
else if (x == 153)
cout << -3736 << ' ' << -695 << ' ' << 3744 << endl;
else if (x == 83)
cout << -3707 << ' ' << 1315 << ' ' << 3651 << endl;
else if (x == 155)
cout << -3693 << ' ' << -1049 << ' ' << 3721 << endl;
else if (x == 119)
cout << -3677 << ' ' << -1673 << ' ' << 3789 << endl;
else if (x == 163)
cout << -3423 << ' ' << -2943 << ' ' << 4033 << endl;
else if (x == 91)
cout << -3389 << ' ' << -2912 << ' ' << 3992 << endl;
else if (x == 17)
cout << -3331 << ' ' << 2195 << ' ' << 2977 << endl;
else if (x == 55)
cout << -3329 << ' ' << 1837 << ' ' << 3131 << endl;
else if (x == 36)
cout << -3223 << ' ' << 2358 << ' ' << 2731 << endl;
else if (x == 97)
cout << -3168 << ' ' << -991 << ' ' << 3200 << endl;
else if (x == 134)
cout << -3013 << ' ' << -1766 << ' ' << 3203 << endl;
else if (x == 109)
cout << -2948 << ' ' << 853 << ' ' << 2924 << endl;
else if (x == 197)
cout << -2867 << ' ' << -1606 << ' ' << 3026 << endl;
else if (x == 179)
cout << -2839 << ' ' << 1503 << ' ' << 2691 << endl;
else if (x == 20)
cout << -2816 << ' ' << -741 << ' ' << 2833 << endl;
else if (x == 120)
cout << -2804 << ' ' << 1219 << ' ' << 2725 << endl;
else if (x == 144)
cout << -2746 << ' ' << -2552 << ' ' << 3342 << endl;
else if (x == 62)
cout << -2744 << ' ' << -1561 << ' ' << 2903 << endl;
else if (x == 106)
cout << -2689 << ' ' << -1165 << ' ' << 2760 << endl;
else if (x == 25)
cout << -2683 << ' ' << 1839 << ' ' << 2357 << endl;
else if (x == 89)
cout << -2514 << ' ' << 1953 << ' ' << 2036 << endl;
else if (x == 53)
cout << -2370 << ' ' << 1518 << ' ' << 2141 << endl;
else if (x == 45)
cout << -2369 << ' ' << 1709 << ' ' << 2025 << endl;
else if (x == 81)
cout << -2368 << ' ' << -1719 << ' ' << 2638 << endl;
else if (x == 147)
cout << -2366 << ' ' << 1528 << ' ' << 2131 << endl;
else if (x == 70)
cout << -2359 << ' ' << 824 << ' ' << 2325 << endl;
else if (x == 101)
cout << -2327 << ' ' << 319 << ' ' << 2325 << endl;
else if (x == 200)
cout << -2318 << ' ' << -638 << ' ' << 2334 << endl;
else if (x == 28)
cout << -2268 << ' ' << -249 << ' ' << 2269 << endl;
else if (x == 126)
cout << -2212 << ' ' << -419 << ' ' << 2217 << endl;
else if (x == 199)
cout << -2208 << ' ' << 508 << ' ' << 2199 << endl;
else if (x == 154)
cout << -2135 << ' ' << -516 << ' ' << 2145 << endl;
else if (x == 26)
cout << -2107 << ' ' << 237 << ' ' << 2106 << endl;
else if (x == 98)
cout << -2101 << ' ' << -1638 << ' ' << 2391 << endl;
else if (x == 78)
cout << -2080 << ' ' << -829 << ' ' << 2123 << endl;
else if (x == 116)
cout << -1906 << ' ' << -757 << ' ' << 1945 << endl;
else if (x == 187)
cout << -1885 << ' ' << -1092 << ' ' << 2000 << endl;
else if (x == 90)
cout << -1803 << ' ' << 365 << ' ' << 1798 << endl;
else if (x == 100)
cout << -1797 << ' ' << -903 << ' ' << 1870 << endl;
else if (x == 189)
cout << -1702 << ' ' << -1403 << ' ' << 1974 << endl;
else if (x == 188)
cout << -1639 << ' ' << 318 << ' ' << 1635 << endl;
else if (x == 129)
cout << -1580 << ' ' << -1238 << ' ' << 1801 << endl;
else if (x == 34)
cout << -1555 << ' ' << -244 << ' ' << 1557 << endl;
else if (x == 159)
cout << -1534 << ' ' << 383 << ' ' << 1526 << endl;
else if (x == 88)
cout << -1390 << ' ' << -1282 << ' ' << 1686 << endl;
else if (x == 18)
cout << -1373 << ' ' << -1276 << ' ' << 1671 << endl;
else if (x == 169)
cout << -1354 << ' ' << -1012 << ' ' << 1521 << endl;
else if (x == 135)
cout << -1351 << ' ' << -629 << ' ' << 1395 << endl;
else if (x == 171)
cout << -1328 << ' ' << 891 << ' ' << 1178 << endl;
else if (x == 82)
cout << -1317 << ' ' << 847 << ' ' << 1188 << endl;
else if (x == 107)
cout << -1309 << ' ' << 947 << ' ' << 1117 << endl;
else if (x == 80)
cout << -1300 << ' ' << -706 << ' ' << 1366 << endl;
else if (x == 60)
cout << -1201 << ' ' << -163 << ' ' << 1202 << endl;
else if (x == 177)
cout << -1168 << ' ' << -160 << ' ' << 1169 << endl;
else if (x == 108)
cout << -1165 << ' ' << -948 << ' ' << 1345 << endl;
else if (x == 35)
cout << -1120 << ' ' << -509 << ' ' << 1154 << endl;
else if (x == 136)
cout << -1116 << ' ' << 816 << ' ' << 946 << endl;
else if (x == 196)
cout << -1057 << ' ' << -579 << ' ' << 1112 << endl;
else if (x == 57)
cout << -998 << ' ' << 361 << ' ' << 982 << endl;
else if (x == 61)
cout << -966 << ' ' << 668 << ' ' << 845 << endl;
else if (x == 65)
cout << -929 << ' ' << 403 << ' ' << 903 << endl;
else if (x == 117)
cout << -896 << ' ' << -555 << ' ' << 962 << endl;
else if (x == 99)
cout << -893 << ' ' << -622 << ' ' << 984 << endl;
else if (x == 43)
cout << -823 << ' ' << -307 << ' ' << 837 << endl;
else if (x == 152)
cout << -805 << ' ' << 486 << ' ' << 741 << endl;
else if (x == 168)
cout << -802 << ' ' << -574 << ' ' << 890 << endl;
else if (x == 51)
cout << -796 << ' ' << 602 << ' ' << 659 << endl;
else if (x == 46)
cout << -758 << ' ' << -473 << ' ' << 815 << endl;
else if (x == 137)
cout << -758 << ' ' << -428 << ' ' << 801 << endl;
else if (x == 79)
cout << -706 << ' ' << -196 << ' ' << 711 << endl;
else if (x == 11)
cout << -695 << ' ' << -641 << ' ' << 843 << endl;
else if (x == 56)
cout << -672 << ' ' << 505 << ' ' << 559 << endl;
else if (x == 10)
cout << -650 << ' ' << -353 << ' ' << 683 << endl;
else if (x == 6)
cout << -637 << ' ' << -205 << ' ' << 644 << endl;
else if (x == 71)
cout << -533 << ' ' << 401 << ' ' << 443 << endl;
else if (x == 151)
cout << -463 << ' ' << 215 << ' ' << 447 << endl;
else if (x == 37)
cout << -444 << ' ' << -84 << ' ' << 445 << endl;
else if (x == 72)
cout << -432 << ' ' << -104 << ' ' << 434 << endl;
else if (x == 69)
cout << -403 << ' ' << 134 << ' ' << 398 << endl;
else if (x == 21)
cout << -401 << ' ' << -287 << ' ' << 445 << endl;
else if (x == 133)
cout << -399 << ' ' << 167 << ' ' << 389 << endl;
else if (x == 191)
cout << -377 << ' ' << -215 << ' ' << 399 << endl;
else if (x == 150)
cout << -367 << ' ' << 260 << ' ' << 317 << endl;
else if (x == 174)
cout << -335 << ' ' << -170 << ' ' << 349 << endl;
else if (x == 73)
cout << -335 << ' ' << -146 << ' ' << 344 << endl;
else if (x == 146)
cout << -327 << ' ' << -263 << ' ' << 376 << endl;
else if (x == 15)
cout << -265 << ' ' << -262 << ' ' << 332 << endl;
else if (x == 93)
cout << -248 << ' ' << -98 << ' ' << 253 << endl;
else if (x == 102)
cout << -239 << ' ' << 118 << ' ' << 229 << endl;
else if (x == 29)
cout << -233 << ' ' << -69 << ' ' << 235 << endl;
else if (x == 9)
cout << -216 << ' ' << -52 << ' ' << 217 << endl;
else if (x == 7)
cout << -169 << ' ' << 44 << ' ' << 168 << endl;
else if (x == 63)
cout << -161 << ' ' << 102 << ' ' << 146 << endl;
else if (x == 47)
cout << -141 << ' ' << 49 << ' ' << 139 << endl;
else if (x == 141)
cout << -139 << ' ' << 104 << ' ' << 116 << endl;
else if (x == 19)
cout << -95 << ' ' << 47 << ' ' << 91 << endl;
else if (x == 182)
cout << -90 << ' ' << -29 << ' ' << 91 << endl;
else if (x == 138)
cout << -86 << ' ' << -77 << ' ' << 103 << endl;
else if (x == 164)
cout << -66 << ' ' << -59 << ' ' << 79 << endl;
else if (x == 123)
cout << -37 << ' ' << -16 << ' ' << 38 << endl;
else if (x == 38)
cout << -27 << ' ' << 16 << ' ' << 25 << endl;
else if (x == 96)
cout << -22 << ' ' << 14 << ' ' << 20 << endl;
else if (x == 192)
cout << -20 << ' ' << 16 << ' ' << 16 << endl;
else if (x == 178)
cout << -13 << ' ' << -10 << ' ' << 15 << endl;
else if (x == 115)
cout << -12 << ' ' << 8 << ' ' << 11 << endl;
else if (x == 12)
cout << -11 << ' ' << 7 << ' ' << 10 << endl;
else if (x == 24)
cout << -10 << ' ' << 8 << ' ' << 8 << endl;
else if (x == 145)
cout << -8 << ' ' << -7 << ' ' << 10 << endl;
else if (x == 44)
cout << -7 << ' ' << -5 << ' ' << 8 << endl;
else if (x == 105)
cout << -7 << ' ' << -4 << ' ' << 8 << endl;
else if (x == 142)
cout << -7 << ' ' << -3 << ' ' << 8 << endl;
else if (x == 3)
cout << -5 << ' ' << 4 << ' ' << 4 << endl;
else if (x == 186)
cout << -4 << ' ' << 5 << ' ' << 5 << endl;
else if (x == 124)
cout << -1 << ' ' << 0 << ' ' << 5 << endl;
else if (x == 132)
cout << -1 << ' ' << 2 << ' ' << 5 << endl;
else if (x == 66)
cout << 1 << ' ' << 1 << ' ' << 4 << endl;
else
cout << "impossible" << endl;
}
return 0;
}
H. Yuuki and a problem
題意: 樹套樹,先鴿,以後補
題解:
程式碼:
J. Loli, Yen-Jen, and a graph problem
題意: 給定一張n個點的完全圖,要求把這張圖分為n-1條路徑,長度分別為1,2,3,…,n-1
題解: 如果n為奇數,那麼每個點的度為偶數,因此必然存在歐拉回路,那麼直接劃分即可;如果n為偶數,那麼可以先1、2,然後選定a,b,連線方式為a-1-b-2-a-b,之後選擇的集合就變成1、2、3、4,那麼繼續a-1-b-2-3-a-4-b-a。每次找到這條路徑之後切成兩份。
程式碼:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
int const MAXN = 1000 + 10, MAXM = MAXN * MAXN;
int n, m, T;
int g[MAXN][MAXN]; // 鄰接表處理
int ans[MAXM << 1], cnt; // 記錄答案
void dfs(int u) {
for (int i = 1; i <= n; ++i) // 遍歷所有的點,如果u->i有路徑
{
if (g[u][i]) // 如果有路徑
{
g[u][i]--, g[i][u]--; // 刪去這條路
dfs(i); // 遍歷對應的點
}
}
ans[++cnt] = u; // 這個點的所有邊對應的所有點都完成dfs後才能把這個點入棧
}
int main() {
cin >> n;
if (n & 1) {
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (i == j) continue;
else g[i][j] = 1;
dfs(1);
int this_cnt = 2;
for (int i = cnt, tot = 0; i >= 1; --i) {
printf("%d ", ans[i]);
tot += 1;
if (tot == this_cnt) {
puts("");
this_cnt ++;
tot = 0;
i ++;
if (i == 2) break;
}
}
}
else {
vector<int> used_point;
int start = 2;
used_point.push_back(1), used_point.push_back(2);
printf("1 2\n");
for (int i = 1; i <= (n - 1) / 2; ++i) {
int a = start + 1, b = start + 2;
start += 2;
vector<int> tmp;
for (int j = 0; j < used_point.size(); ++j) {
if (j & 1) tmp.push_back(b);
else tmp.push_back(a);
tmp.push_back(used_point[j]);
}
tmp.push_back(a), tmp.push_back(b);
int len = tmp.size();
for (int j = 0; j < tmp.size(); ++j) {
printf("%d", tmp[j]);
if (j == len / 2 - 1) printf("\n%d ", tmp[j]);
else printf(" ");
}
printf("\n");
used_point.push_back(a), used_point.push_back(b);
}
}
return 0;
}
L. Loli, Yen-Jen, and a cool problem
題意: 廣義sam,先鴿了,以後再補
題解:
程式碼:
M. Kill the tree
題意: 求一棵樹所有子樹的重心
題解: cf基本原題:Codeforces Round #359 (Div. 2) D - Kay and Snowflake
程式碼:
#include <bits/stdc++.h>
using namespace std;
int const N = 2e5 + 10, M = N * 2;
int n, q, e[M], ne[M], h[N], sz[N], idx, zson[N], father[N]; // sz[u]維護u的子樹大小, zson[u]維護u的重兒子
int ans[N], ans2[N];
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int dfs(int u, int fa) {
father[u] = fa;
sz[u] = 1;
for (int i = h[u] ;~i; i = ne[i]) {
int j = e[i];
if (j == fa) continue;
dfs(j, u);
sz[u] += sz[j];
if (sz[zson[u]] < sz[j]) zson[u] = j; // 更新重兒子
}
ans[u] = u; // 樹u的重心必然在u重兒子子樹重心到u之間
if (zson[u]) { // 存在重兒子
int p = ans[zson[u]]; // 找到重兒子的重心
while(p != u && ((sz[u] - sz[p]) * 2 > sz[u])) p = father[p]; // 找到第一個滿足這個條件的點,如果這個點不滿足為重心,後面更不滿足
ans[u] = p;
if ((sz[u] - sz[p]) * 2 == sz[u]) ans2[u] = father[p];
}
return sz[u];
}
int main() {
cin >> n;
memset(h, -1, sizeof h);
for (int i = 2, a, b; i <= n; ++i) {
scanf("%d%d", &a, &b);
add(a, b), add(b, a);
}
dfs(1, -1);
for (int i = 1; i <= n; ++i) {
if (!ans2[i]) printf("%d\n", ans[i]);
else printf("%d %d\n", min(ans[i], ans2[i]), max(ans[i], ans2[i]));
}
return 0;
}