답안 #88829

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
88829 2018-12-09T03:45:28 Z LittleFlowers__ Deblo (COCI18_deblo) C++14
72 / 90
1000 ms 29596 KB
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <math.h>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <climits>
#include <random>
#include <chrono>
#include <typeinfo>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int SIZE = 1 << 10;
int pointer = SIZE;
char buffer[SIZE];
inline char readchar(){if (pointer == SIZE){fread(buffer, 1, SIZE, stdin);pointer = 0;}return buffer[pointer++];}
inline int in(){int x=0;char c=readchar();bool neg=false;while(c!='-'&&('0'>c||c>'9')) c=readchar();if(c=='-') neg=true,c=readchar();while('0'<=c&&c<='9') x=10*x+c-'0',c=readchar();if(neg) x=-x;return x;}
inline void out(long long x){if(x<0) putchar('-'),x=-x;if(x>9) out(x/10);putchar(x%10+'0');}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int l,int r){return l+rng()%(r-l+1);}
#define task "deblo"
#define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
#define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define nd second.first
#define rd second.second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vii vector<ii>
#define ii pair<int,int>
#define all(a) a.begin(),a.end()
#define sz(s) int(s.size())
#define reset(f, x) memset(f, x, sizeof(f))
#define bit(x,i) ((x>>(i-1))&1)
#define on(x,i) (x|(1ll<<(i-1)))
#define off(x,i) (x&~(1<<(i-1)))
#define _ putchar(' ')
#define __ putchar('\n')
const int N=100010;
int n,cnt,root,pw;
long long kq,re;
int c[N],g[2],f[2];
int a[N][25];
bool dd[N];
vi ad[N],nex[N];
void dfs(int u,int p)
{
    c[u]=1;cnt++;
    forv(v,ad[u]) if(!dd[v] && v!=p) dfs(v,u),c[u]+=c[v];
}
int ftr(int u,int p)
{
    forv(v,ad[u]) if(!dd[v] && v!=p && c[v]>cnt/2) return ftr(v,u);
    return u;
}
void ctr(int u,int p)
{
    cnt=0;dfs(u,u);
    int z=ftr(u,u);
    dd[z]=1;
    if(u==p) root=z;
    if(u!=p) nex[p].pb(z);
    forv(v,ad[z]) if(!dd[v]) ctr(v,z);
}
void dfs(int u,int p,int r,int c)
{
    forv(v,ad[u]) if(!dd[v] && v!=p)
    {
        f[a[v][pw]^c]++;
        dfs(v,u,r,a[v][pw]^c);
        if(u==r)
        {
            re+=f[0]*g[1-a[u][pw]]+f[1]*g[a[u][pw]]+f[1-a[u][pw]];
            g[0]+=f[0],g[1]+=f[1],f[0]=f[1]=0;
        }
    }
}
void dp(int u)
{
    dd[u]=1;
    f[0]=f[1]=g[0]=g[1]=0;
    dfs(u,u,u,0);re+=a[u][pw];
    forv(v,nex[u]) dp(v);
}
int main()
{
    n=in();
    forinc(i,1,n)
    {
        int x;x=in();
        forinc(j,1,22) a[i][j]=bit(x,j);
    }
    forinc(i,1,n-1)
    {
        int u,v;u=in(),v=in();
        ad[u].pb(v);
        ad[v].pb(u);
    }
    ctr(1,1);
    forinc(i,1,22)
    {
        reset(dd,0);
        re=0,pw=i;
        dp(root);
        kq+=re*(1<<i-1);
    }
    out(kq);
}

Compilation message

deblo.cpp: In function 'int main()':
deblo.cpp:115:21: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
         kq+=re*(1<<i-1);
                    ~^~
deblo.cpp: In function 'char readchar()':
deblo.cpp:23:50: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
 inline char readchar(){if (pointer == SIZE){fread(buffer, 1, SIZE, stdin);pointer = 0;}return buffer[pointer++];}
                                             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5240 KB Output is correct
3 Correct 6 ms 5448 KB Output is correct
4 Correct 10 ms 5636 KB Output is correct
5 Correct 9 ms 5636 KB Output is correct
6 Correct 731 ms 27404 KB Output is correct
7 Correct 719 ms 29224 KB Output is correct
8 Correct 993 ms 29224 KB Output is correct
9 Execution timed out 1022 ms 29224 KB Time limit exceeded
10 Execution timed out 1064 ms 29596 KB Time limit exceeded