답안 #566896

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
566896 2022-05-23T05:46:11 Z azberjibiou Star Trek (CEOI20_startrek) C++17
0 / 100
2 ms 2676 KB
#include <bits/stdc++.h>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fir first
#define sec second
#define pdd pair<long double, long double>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ld long double
#define pmax pair<__int128, __int128>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
int dx[4]= {0, 1, 0, -1}, dy[4]= {1, 0, -1, 0};
int ddx[8]={1, 1, 0, -1, -1, -1, 0, 1}, ddy[8]={0, 1, 1, 1, 0, -1, -1, -1};
const int mxN=100025;
const int mxM=300005;
const int mxK=1000000000;
const ll MOD=1000'000'007;
const ll INF=1000000000000000005;
ll N, D;
vector <int> v[mxN];
ll outw[mxN];
ll downw[mxN], upw[mxN];
ll degL[mxN];
bool W[mxN];
ll numw;
void dfs_downw(int now, int pre)
{
    for(int nxt : v[now])   if(nxt!=pre)
    {
        dfs_downw(nxt, now);
    }
    downw[now]=(outw[now]==0 ? 1 : 0);
    if(now!=1)  outw[pre]+=downw[now];
}
void dfs_upw(int now, int pre)
{
    if(now!=1)
    {
        upw[now]=(outw[pre]-downw[now]==0 ? 1 : 0);
        outw[now]+=upw[now];
    }
    for(int nxt : v[now])   if(nxt!=pre)
    {
        dfs_upw(nxt, now);
    }
}
ll dfs1(int now, int pre, int dep)
{
    ll sum=0;
    bool ok=true;
    for(int nxt : v[now])   if(W[nxt] && nxt!=pre && degL[nxt]==0)
    {
        ok=false;
        sum+=dfs1(nxt, now, 1-dep);
    }
    if(sum==0 && !ok)   return 0;
    if(!ok) return sum+1;
    if(ok)
    {
        return dep;
    }
}
ll dfs2(int now, int pre)
{
    ll sum=!W[now];
    for(int nxt : v[now])   if(nxt!=pre && W[nxt]+W[now]==1)
    {
        sum+=dfs2(nxt, now);

    }
    return sum;
}
int main()
{
    gibon
    cin >> N >> D;
    for(int i=1;i<N;i++)
    {
        int a, b;   cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs_downw(1, -1);
    dfs_upw(1, -1);
    for(int i=1;i<=N;i++)   if(outw[i]>=1)  W[i]=true;
    for(int i=1;i<=N;i++)   numw+=W[i];
    for(int i=1;i<=N;i++)   for(int ele : v[i]) degL[i]+=1-W[ele];
    ll ans=0;
    if(W[1])
    {
        ans=N*N;
        if(degL[1]==0)
        {
            ans-=dfs1(1, -1, 0)/2*(N-numw);
        }
        if(degL[1]==1)
        {
            ans-=dfs2(1, -1)*(N-numw);
        }
    }
    else
    {
        ans+=dfs2(1, -1)*(N-numw);
    }
    cout << ans;
}
/*
10 1
10 1
1 8
8 3
3 6
6 7
6 5
6 2
2 9
9 4*/

Compilation message

startrek.cpp: In function 'll dfs1(int, int, int)':
startrek.cpp:65:1: warning: control reaches end of non-void function [-Wreturn-type]
   65 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Incorrect 2 ms 2676 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2676 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Incorrect 2 ms 2672 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2676 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Incorrect 2 ms 2672 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2676 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Incorrect 2 ms 2672 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2676 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Incorrect 2 ms 2672 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2676 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Incorrect 2 ms 2672 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Incorrect 2 ms 2676 KB Output isn't correct
3 Halted 0 ms 0 KB -