#define DEBUG 1
#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 x[1000000];
int sum[1000000];
int dp2[1010][1010];
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);
}
doDFS(t,-1);
int j,k;
int u = m,p = -1,c = 0;
while (u != -1) {
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if ((v != parent[u]) && (v != p)) x[c].pb(dp[v]);
}
sort(x[c].begin(),x[c].end(),greater<int>());
sum[c] += adjList[u].size()-2;
p = u,u = parent[u],c++;
}
reverse(x,x+c),reverse(sum,sum+c);
for (i = 1; i < c; i++) {
if (i > 1) sum[i] += sum[i-1];
for (j = 0; j <= n; j++) {
dp2[i][j] = MAX_VAL;
for (k = 0; k <= x[i].size(); k++) {
if (k > j+1) break;
dp2[i][j] = min(dp2[i][j],max((k == x[i].size()) ? 0:(x[i][k]+sum[i]+1),dp2[i-1][j+1-k]+k));
}
}
}
printf("%d\n",dp2[c-1][0]);
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:96:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
mousetrap.cpp:109:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (k = 0; k <= x[i].size(); k++) {
~~^~~~~~~~~~~~~~
mousetrap.cpp:111:50: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
dp2[i][j] = min(dp2[i][j],max((k == x[i].size()) ? 0:(x[i][k]+sum[i]+1),dp2[i-1][j+1-k]+k));
~~^~~~~~~~~~~~~~
mousetrap.cpp:84: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:86:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&a,&b);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
47352 KB |
Output is correct |
2 |
Correct |
45 ms |
47356 KB |
Output is correct |
3 |
Correct |
44 ms |
47392 KB |
Output is correct |
4 |
Correct |
44 ms |
47392 KB |
Output is correct |
5 |
Correct |
44 ms |
47392 KB |
Output is correct |
6 |
Correct |
44 ms |
47392 KB |
Output is correct |
7 |
Correct |
44 ms |
47424 KB |
Output is correct |
8 |
Incorrect |
45 ms |
47440 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
483 ms |
90404 KB |
Output is correct |
2 |
Correct |
457 ms |
90404 KB |
Output is correct |
3 |
Correct |
1097 ms |
91752 KB |
Output is correct |
4 |
Correct |
483 ms |
91752 KB |
Output is correct |
5 |
Correct |
1110 ms |
91752 KB |
Output is correct |
6 |
Correct |
1098 ms |
91756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
47352 KB |
Output is correct |
2 |
Correct |
45 ms |
47356 KB |
Output is correct |
3 |
Correct |
44 ms |
47392 KB |
Output is correct |
4 |
Correct |
44 ms |
47392 KB |
Output is correct |
5 |
Correct |
44 ms |
47392 KB |
Output is correct |
6 |
Correct |
44 ms |
47392 KB |
Output is correct |
7 |
Correct |
44 ms |
47424 KB |
Output is correct |
8 |
Incorrect |
45 ms |
47440 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
47352 KB |
Output is correct |
2 |
Correct |
45 ms |
47356 KB |
Output is correct |
3 |
Correct |
44 ms |
47392 KB |
Output is correct |
4 |
Correct |
44 ms |
47392 KB |
Output is correct |
5 |
Correct |
44 ms |
47392 KB |
Output is correct |
6 |
Correct |
44 ms |
47392 KB |
Output is correct |
7 |
Correct |
44 ms |
47424 KB |
Output is correct |
8 |
Incorrect |
45 ms |
47440 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |