#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int t,m;
vi adjList[1000000];
int parent[1000000],dp[1000000];
int doDFS(int u,int p) {
int i;
int m1 = 0,m2 = 0,c = 0;
parent[u] = p;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) {
doDFS(v,u);
if (dp[v] >= m1) m2 = m1,m1 = dp[v];
else if (dp[v] >= m2) m2 = dp[v];
c++;
}
}
dp[u] = m2+c;
return dp[u];
}
vi w[1000000];
int sum[1000000];
int main() {
int i;
int n;
int a,b;
scanf("%d %d %d",&n,&t,&m),t--,m--;
for (i = 1; i < n; i++) {
scanf("%d %d",&a,&b);
a--,b--;
adjList[a].pb(b);
adjList[b].pb(a);
}
int j;
int u = m,p = -1,c = 0;
doDFS(t,-1);
while (u != t) {
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if ((v != parent[u]) && (v != p)) w[c].pb(dp[v]),sum[c]++;
}
sort(w[c].begin(),w[c].end(),greater<int>());
p = u,u = parent[u],c++;
}
c++;
reverse(w,w+c),reverse(sum,sum+c);
for (i = 1; i < c; i++) sum[i] += sum[i-1];
int l = 0,r = n;
while (l < r) {
int m = (l+r) / 2;
int used = 0,can = 0;
for (i = c-1; i >= 1; i--) {
can++;
int o = used;
for (j = 0; j < w[i].size(); j++) {
if (w[i][j]+sum[i]+o > m) {
used++,can--;
if (can < 0) break;
}
}
if (j < w[i].size()) break;
}
if ((i == 0) && (used <= m)) r = m;
else l = m+1;
}
printf("%d\n",l);
return 0;
}
Compilation message
mousetrap.cpp: In function 'int doDFS(int, int)':
mousetrap.cpp:65:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:95:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
mousetrap.cpp:112:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < w[i].size(); j++) {
~~^~~~~~~~~~~~~
mousetrap.cpp:118:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (j < w[i].size()) break;
~~^~~~~~~~~~~~~
mousetrap.cpp:83:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&n,&t,&m),t--,m--;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
mousetrap.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&a,&b);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
47224 KB |
Output is correct |
2 |
Correct |
44 ms |
47476 KB |
Output is correct |
3 |
Correct |
44 ms |
47476 KB |
Output is correct |
4 |
Correct |
43 ms |
47476 KB |
Output is correct |
5 |
Correct |
44 ms |
47480 KB |
Output is correct |
6 |
Correct |
46 ms |
47480 KB |
Output is correct |
7 |
Correct |
44 ms |
47620 KB |
Output is correct |
8 |
Correct |
44 ms |
47620 KB |
Output is correct |
9 |
Correct |
43 ms |
47620 KB |
Output is correct |
10 |
Correct |
44 ms |
47620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
466 ms |
86684 KB |
Output is correct |
2 |
Correct |
410 ms |
86684 KB |
Output is correct |
3 |
Correct |
1038 ms |
87760 KB |
Output is correct |
4 |
Correct |
476 ms |
87760 KB |
Output is correct |
5 |
Correct |
1014 ms |
87836 KB |
Output is correct |
6 |
Correct |
1020 ms |
87908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
47224 KB |
Output is correct |
2 |
Correct |
44 ms |
47476 KB |
Output is correct |
3 |
Correct |
44 ms |
47476 KB |
Output is correct |
4 |
Correct |
43 ms |
47476 KB |
Output is correct |
5 |
Correct |
44 ms |
47480 KB |
Output is correct |
6 |
Correct |
46 ms |
47480 KB |
Output is correct |
7 |
Correct |
44 ms |
47620 KB |
Output is correct |
8 |
Correct |
44 ms |
47620 KB |
Output is correct |
9 |
Correct |
43 ms |
47620 KB |
Output is correct |
10 |
Correct |
44 ms |
47620 KB |
Output is correct |
11 |
Correct |
43 ms |
87908 KB |
Output is correct |
12 |
Correct |
43 ms |
87908 KB |
Output is correct |
13 |
Correct |
44 ms |
87908 KB |
Output is correct |
14 |
Correct |
45 ms |
87908 KB |
Output is correct |
15 |
Correct |
43 ms |
87908 KB |
Output is correct |
16 |
Correct |
45 ms |
87908 KB |
Output is correct |
17 |
Correct |
45 ms |
87908 KB |
Output is correct |
18 |
Correct |
44 ms |
87908 KB |
Output is correct |
19 |
Correct |
44 ms |
87908 KB |
Output is correct |
20 |
Correct |
44 ms |
87908 KB |
Output is correct |
21 |
Correct |
45 ms |
87908 KB |
Output is correct |
22 |
Correct |
44 ms |
87908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
47224 KB |
Output is correct |
2 |
Correct |
44 ms |
47476 KB |
Output is correct |
3 |
Correct |
44 ms |
47476 KB |
Output is correct |
4 |
Correct |
43 ms |
47476 KB |
Output is correct |
5 |
Correct |
44 ms |
47480 KB |
Output is correct |
6 |
Correct |
46 ms |
47480 KB |
Output is correct |
7 |
Correct |
44 ms |
47620 KB |
Output is correct |
8 |
Correct |
44 ms |
47620 KB |
Output is correct |
9 |
Correct |
43 ms |
47620 KB |
Output is correct |
10 |
Correct |
44 ms |
47620 KB |
Output is correct |
11 |
Correct |
466 ms |
86684 KB |
Output is correct |
12 |
Correct |
410 ms |
86684 KB |
Output is correct |
13 |
Correct |
1038 ms |
87760 KB |
Output is correct |
14 |
Correct |
476 ms |
87760 KB |
Output is correct |
15 |
Correct |
1014 ms |
87836 KB |
Output is correct |
16 |
Correct |
1020 ms |
87908 KB |
Output is correct |
17 |
Correct |
43 ms |
87908 KB |
Output is correct |
18 |
Correct |
43 ms |
87908 KB |
Output is correct |
19 |
Correct |
44 ms |
87908 KB |
Output is correct |
20 |
Correct |
45 ms |
87908 KB |
Output is correct |
21 |
Correct |
43 ms |
87908 KB |
Output is correct |
22 |
Correct |
45 ms |
87908 KB |
Output is correct |
23 |
Correct |
45 ms |
87908 KB |
Output is correct |
24 |
Correct |
44 ms |
87908 KB |
Output is correct |
25 |
Correct |
44 ms |
87908 KB |
Output is correct |
26 |
Correct |
44 ms |
87908 KB |
Output is correct |
27 |
Correct |
45 ms |
87908 KB |
Output is correct |
28 |
Correct |
44 ms |
87908 KB |
Output is correct |
29 |
Correct |
44 ms |
87908 KB |
Output is correct |
30 |
Correct |
462 ms |
87908 KB |
Output is correct |
31 |
Correct |
468 ms |
87908 KB |
Output is correct |
32 |
Correct |
550 ms |
168828 KB |
Output is correct |
33 |
Correct |
503 ms |
178428 KB |
Output is correct |
34 |
Correct |
987 ms |
178428 KB |
Output is correct |
35 |
Correct |
1049 ms |
178428 KB |
Output is correct |
36 |
Correct |
1014 ms |
178428 KB |
Output is correct |
37 |
Correct |
1044 ms |
178428 KB |
Output is correct |
38 |
Correct |
829 ms |
180580 KB |
Output is correct |
39 |
Correct |
815 ms |
194004 KB |
Output is correct |
40 |
Correct |
827 ms |
207360 KB |
Output is correct |