이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define repa(i,a,b) for(int i = (a); i >= (b); i--)
#define debugsl(a) cout << #a << " = " << a << ", "
#define debug(a) cout << #a << " = " << a << endl;
#define MAX 100000
#define mod 1000000007
bool si;
lli n,d,a,b,total,g,p;
vector<lli> hijos[MAX+2];
lli estado[MAX+2],original[MAX+2];
lli DFS (lli pos, lli padre) {
lli res = 1;
for (auto h : hijos[pos]) {
if (h == padre) continue;
res *= DFS(h,pos);
}
if (res == 0) {
if (si) original[pos] = 1;
return 1;
}
else {
if (si) original[pos] = 0;
return 0;
}
}
void resuelve(lli pos, lli padre, lli prof) {
if (prof%2 == 0) total += p;
else if (original[pos] == 0) total += g;
if (total > mod) total %= mod;
lli ap = 0;
lli ag = 0;
for(auto h : hijos[pos]) {
if (h == padre) continue;
if (original[h] == 0) ap++;
else ag++;
}
if (prof%2 && original[pos] == 1 && ap > 1) return;
for(auto h : hijos[pos]) {
if (h == padre) continue;
if (prof%2 && original[pos] == 1 && original[h] == 0) resuelve(h,pos,prof+1);
else if (prof%2 && original[pos] == 0) resuelve(h,pos,prof+1);
else if (!(prof%2)) resuelve(h,pos,prof+1);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> d;
rep(i,1,n-1) {
cin >> a >> b;
hijos[a].push_back(b);
hijos[b].push_back(a);
}
si = true;
rep(i,1,n) {
estado[i] = DFS(i,0);
si = false;
if (estado[i] == 0) p++;
else g++;
}
total = 0;
resuelve(1,0,0);
cout << total;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |