이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 2e5+3, oo = 1e9;
int choice[mxN],d[mxN],N,D, minput[mxN];
vi adj[mxN], ans;
void dfs(int at, int from) {
choice[at]=at;
minput[at]=oo;
for(int to : adj[at]) if(to!=from) {
d[to] = d[at]+1;
dfs(to,at);
minput[at] = min(minput[at],minput[to]+1);
if(choice[to]) {
if((d[choice[to]]-d[at])*2>=D) {
ans.push_back(choice[to]);
minput[at] = min(minput[at], d[choice[to]]-d[at]);
} else if(d[choice[to]]>d[choice[at]]) {
choice[at] = choice[to];
}
}
}
int got = d[choice[at]]-d[at];
if(got+minput[at]<D) {
choice[at]=0;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> D;
for(int i=0;i<N-1;++i) {
int u,v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1,0);
if(choice[1]) ans.push_back(choice[1]);
cout << ans.size() << '\n'; //<< ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |