Language: C++

#include<queue> #include<cstdio>
#include<algorithm>
using namespace std; const int MAXN = 2000; struct edge{ int to; edge *nxt; }edges[MAXN*2 + 5], *adj[MAXN + 5], *ecnt = &edges[0]; void addedge(int u, int v) { edge *p = (++ecnt); p->to = v, p->nxt = adj[u], adj[u] = p; p = (++ecnt); p->to = u, p->nxt = adj[v], adj[v] = p; } queue<int>que; int dis[MAXN + 5], vis[MAXN + 5], N, K; int solve() { int ret = 0; while( !que.empty() ) { int f = que.front(); que.pop(); if( dis[f] > K/2 ) ret++; for(edge *p=adj[f];p!=NULL;p=p->nxt) { if( !vis[p->to] ) { vis[p->to] = true; dis[p->to] = dis[f] + 1; que.push(p->to); } } } return ret; } int main() { scanf("%d%d", &N, &K); for(int i=1;i<N;i++) { int A, B; scanf("%d%d", &A, &B); addedge(A, B); } int ans = MAXN + 5; if( K % 2 == 0 ) { for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) { vis[j] = false; dis[j] = 0; } vis[i] = true; que.push(i); ans = min(ans, solve()); } } else { for(int i=1;i<=N;i++) { for(edge *p=adj[i];p!=NULL;p=p->nxt) { if( i > p->to ) continue; for(int j=1;j<=N;j++) { vis[j] = false; dis[j] = 0; } vis[i] = true; que.push(i); vis[p->to] = true; que.push(p->to); ans = min(ans, solve()); } } } printf("%d\n", ans); }


Compiler output:
a.cc:1:17: warning: extra tokens at end of #include directive
    1 | #include<queue> #include<cstdio>
      |                 ^
a.cc: In function 'int main()':
a.cc:3:594: error: 'scanf' was not declared in this scope
    3 | using namespace std; const int MAXN = 2000; struct edge{ int to; edge *nxt; }edges[MAXN*2 + 5], *adj[MAXN + 5], *ecnt = &edges[0]; void addedge(int u, int v) { edge *p = (++ecnt); p->to = v, p->nxt = adj[u], adj[u] = p; p = (++ecnt); p->to = u, p->nxt = adj[v], adj[v] = p; } queue<int>que; int dis[MAXN + 5], vis[MAXN + 5], N, K; int solve() { int ret = 0; while( !que.empty() ) { int f = que.front(); que.pop(); if( dis[f] > K/2 ) ret++; for(edge *p=adj[f];p!=NULL;p=p->nxt) { if( !vis[p->to] ) { vis[p->to] = true; dis[p->to] = dis[f] + 1; que.push(p->to); } } } return ret; } int main() { scanf("%d%d", &N, &K); for(int i=1;i<N;i++) { int A, B; scanf("%d%d", &A, &B); addedge(A, B); } int ans = MAXN + 5; if( K % 2 == 0 ) { for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) { vis[j] = false; dis[j] = 0; } vis[i] = true; que.push(i); ans = min(ans, solve()); } } else { for(int i=1;i<=N;i++) { for(edge *p=adj[i];p!=NULL;p=p->nxt) { if( i > p->to ) continue; for(int j=1;j<=N;j++) { vis[j] = false; dis[j] = 0; } vis[i] = true; que.push(i); vis[p->to] = true; que.push(p->to); ans = min(ans, solve()); } } } printf("%d\n", ans); }
      |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ^~~~~
a.cc:3:1109: error: 'printf' was not declared in this scope
    3 | using namespace std; const int MAXN = 2000; struct edge{ int to; edge *nxt; }edges[MAXN*2 + 5], *adj[MAXN + 5], *ecnt = &edges[0]; void addedge(int u, int v) { edge *p = (++ecnt); p->to = v, p->nxt = adj[u], adj[u] = p; p = (++ecnt); p->to = u, p->nxt = adj[v], adj[v] = p; } queue<int>que; int dis[MAXN + 5], vis[MAXN + 5], N, K; int solve() { int ret = 0; while( !que.empty() ) { int f = que.front(); que.pop(); if( dis[f] > K/2 ) ret++; for(edge *p=adj[f];p!=NULL;p=p->nxt) { if( !vis[p->to] ) { vis[p->to] = true; dis[p->to] = dis[f] + 1; que.push(p->to); } } } return ret; } int main() { scanf("%d%d", &N, &K); for(int i=1;i<N;i++) { int A, B; scanf("%d%d", &A, &B); addedge(A, B); } int ans = MAXN + 5; if( K % 2 == 0 ) { for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) { vis[j] = false; dis[j] = 0; } vis[i] = true; que.push(i); ans = min(ans, solve()); } } else { for(int i=1;i<=N;i++) { for(edge *p=adj[i];p!=NULL;p=p->nxt) { if( i > p->to ) continue; for(int j=1;j<=N;j++) { vis[j] = false; dis[j] = 0; } vis[i] = true; que.push(i); vis[p->to] = true; que.push(p->to); ans = min(ans, solve()); } } } printf("%d\n", ans); }
      |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     ^~~~~~
a.cc:3:1: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
    2 | #include<algorithm>
  +++ |+#include <cstdio>
    3 | using namespace std; const int MAXN = 2000; struct edge{ int to; edge *nxt; }edges[MAXN*2 + 5], *adj[MAXN + 5], *ecnt = &edges[0]; void addedge(int u, int v) { edge *p = (++ecnt); p->to = v, p->nxt = adj[u], adj[u] = p; p = (++ecnt); p->to = u, p->nxt = adj[v], adj[v] = p; } queue<int>que; int dis[MAXN + 5], vis[MAXN + 5], N, K; int solve() { int ret = 0; while( !que.empty() ) { int f = que.front(); que.pop(); if( dis[f] > K/2 ) ret++; for(edge *p=adj[f];p!=NULL;p=p->nxt) { if( !vis[p->to] ) { vis[p->to] = true; dis[p->to] = dis[f] + 1; que.push(p->to); } } } return ret; } int main() { scanf("%d%d", &N, &K); for(int i=1;i<N;i++) { int A, B; scanf("%d%d", &A, &B); addedge(A, B); } int ans = MAXN + 5; if( K % 2 == 0 ) { for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) { vis[j] = false; dis[j] = 0; } vis[i] = true; que.push(i); ans = min(ans, solve()); } } else { for(int i=1;i<=N;i++) { for(edge *p=adj[i];p!=NULL;p=p->nxt) { if( i > p->to ) continue; for(int j=1;j<=N;j++) { vis[j] = false; dis[j] = 0; } vis[i] = true; que.push(i); vis[p->to] = true; que.push(p->to); ans = min(ans, solve()); } } } printf("%d\n", ans); }