Submission #2501802


Source Code Expand

/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
VI e[SIZE];
char s[SIZE];
int have_w[SIZE];
int bw[SIZE];
int base;
void dfs0(int x,int lt){
    if(s[x]=='W')have_w[x]=1;
    for(int y: e[x]){
        if(y==lt)continue;
        dfs0(y,x);
        if(have_w[y]){
            have_w[x]=1;
            base+=2;
            bw[x]^=1;
        }
    }
    if(x!=lt&&have_w[x])bw[x]^=1;
    if(!bw[x])base++;
}
int dp[SIZE][2];
int ans;
const int INF = 1e8;
void dfs(int x,int lt){
    dp[x][1]=-INF;
    dp[x][0]=-INF;
    for(int y:e[x]){
        if(y==lt)continue;
        if(!have_w[y])continue;
        dfs(y,x);
        ans=min(ans,base-dp[y][1]);
        ans=min(ans,base-dp[y][0]+bw[x]);
        ans=min(ans,base-dp[x][0]-dp[y][1]+(2*bw[x]-1));
        ans=min(ans,base-dp[x][1]-dp[y][0]+(2*bw[x]-1));
        REP(j,2)dp[x][j]=max(dp[x][j],dp[y][j]);
    }
    REP(i,2)dp[x][i]+=1-(2*bw[x]-1);
    dp[x][1]=max(dp[x][1],1-(2*bw[x]-1));
    dp[x][0]=max(dp[x][0],1);
}
int main(){
    int N; R(N);
    REPP(i,1,N){
        int x,y;
        R(x,y);
        e[x].PB(y);
        e[y].PB(x);
    }
    RS(s+1);
    int w_cnt=0;
    FOR(i,1,N){
        if(s[i]=='W')w_cnt++;
    }
    if(w_cnt <= 1){
        W(w_cnt);
        return 0;
    }
    int st;
    FOR(i,1,N){
        if(s[i]=='W'){
            st=i;
        }
        else bw[i]=1;
    }
    dfs0(st,st);
    ans=INF;
    dfs(st,st);
    W(ans);
    return 0;
}

Submission Info

Submission Time
Task F - Monochrome Cat
User dreamoon
Language C++14 (GCC 5.4.1)
Score 800
Code Size 3957 Byte
Status AC
Exec Time 62 ms
Memory 42240 KB

Compile Error

./Main.cpp: In function ‘void _R(int64_t&)’:
./Main.cpp:46:39: warning: format ‘%lld’ expects argument of type ‘long long int*’, but argument 2 has type ‘int64_t* {aka long int*}’ [-Wformat=]
 void _R(int64_t &x) { scanf("%lld", &x); }
                                       ^
./Main.cpp: In function ‘void _W(const int64_t&)’:
./Main.cpp:54:45: warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘int64_t {aka long int}’ [-Wformat=]
 void _W(const int64_t &x) { printf("%lld", x); }
                                             ^
./Main.cpp: In function ‘void _R(int&)’:
./Main.cpp:45:34: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
 void _R(int &x) { scanf("%d", &x); }
                                  ^
./Main.cpp: In function ‘void _R(int64_t&)’:
./Main.cpp:46:40: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
 voi...

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 4
AC × 118
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt, 1_033.txt, 1_034.txt, 1_035.txt, 1_036.txt, 1_037.txt, 1_038.txt, 1_039.txt, 1_040.txt, 1_041.txt, 1_042.txt, 1_043.txt, 1_044.txt, 1_045.txt, 1_046.txt, 1_047.txt, 1_048.txt, 1_049.txt, 1_050.txt, 1_051.txt, 1_052.txt, 1_053.txt, 1_054.txt, 1_055.txt, 1_056.txt, 1_057.txt, 1_058.txt, 1_059.txt, 1_060.txt, 1_061.txt, 1_062.txt, 1_063.txt, 1_064.txt, 1_065.txt, 1_066.txt, 1_067.txt, 1_068.txt, 1_069.txt, 1_070.txt, 1_071.txt, 1_072.txt, 1_073.txt, 1_074.txt, 1_075.txt, 1_076.txt, 1_077.txt, 1_078.txt, 1_079.txt, 1_080.txt, 1_081.txt, 1_082.txt, 1_083.txt, 1_084.txt, 1_085.txt, 1_086.txt, 1_087.txt, 1_088.txt, 1_089.txt, 1_090.txt, 1_091.txt, 1_092.txt, 1_093.txt, 1_094.txt, 1_095.txt, 1_096.txt, 1_097.txt, 1_098.txt, 1_099.txt, 1_100.txt, 1_101.txt, 1_102.txt, 1_103.txt, 1_104.txt, 1_105.txt, 1_106.txt, 1_107.txt, 1_108.txt, 1_109.txt, 1_110.txt, 1_111.txt, 1_112.txt, 1_113.txt, 1_114.txt, 1_115.txt, 1_116.txt
Case Name Status Exec Time Memory
0_000.txt AC 10 ms 30080 KB
0_001.txt AC 10 ms 30080 KB
0_002.txt AC 9 ms 25984 KB
0_003.txt AC 10 ms 30080 KB
1_003.txt AC 9 ms 25984 KB
1_004.txt AC 9 ms 25984 KB
1_005.txt AC 9 ms 25984 KB
1_006.txt AC 9 ms 25984 KB
1_007.txt AC 9 ms 25984 KB
1_008.txt AC 10 ms 30080 KB
1_009.txt AC 10 ms 30080 KB
1_010.txt AC 10 ms 30080 KB
1_011.txt AC 9 ms 25984 KB
1_012.txt AC 9 ms 25984 KB
1_013.txt AC 10 ms 30080 KB
1_014.txt AC 10 ms 30080 KB
1_015.txt AC 10 ms 30080 KB
1_016.txt AC 10 ms 30080 KB
1_017.txt AC 9 ms 25984 KB
1_018.txt AC 9 ms 25984 KB
1_019.txt AC 10 ms 30080 KB
1_020.txt AC 9 ms 25984 KB
1_021.txt AC 10 ms 30080 KB
1_022.txt AC 10 ms 30080 KB
1_023.txt AC 9 ms 25984 KB
1_024.txt AC 9 ms 25984 KB
1_025.txt AC 10 ms 30080 KB
1_026.txt AC 10 ms 30080 KB
1_027.txt AC 10 ms 30080 KB
1_028.txt AC 10 ms 30080 KB
1_029.txt AC 9 ms 25984 KB
1_030.txt AC 9 ms 25984 KB
1_031.txt AC 10 ms 30080 KB
1_032.txt AC 10 ms 30080 KB
1_033.txt AC 10 ms 30080 KB
1_034.txt AC 10 ms 30080 KB
1_035.txt AC 9 ms 25984 KB
1_036.txt AC 10 ms 25984 KB
1_037.txt AC 10 ms 30080 KB
1_038.txt AC 10 ms 30080 KB
1_039.txt AC 10 ms 30080 KB
1_040.txt AC 10 ms 30080 KB
1_041.txt AC 9 ms 25984 KB
1_042.txt AC 9 ms 25984 KB
1_043.txt AC 10 ms 30080 KB
1_044.txt AC 10 ms 30080 KB
1_045.txt AC 27 ms 32640 KB
1_046.txt AC 31 ms 33280 KB
1_047.txt AC 44 ms 29056 KB
1_048.txt AC 35 ms 28288 KB
1_049.txt AC 43 ms 37248 KB
1_050.txt AC 39 ms 35200 KB
1_051.txt AC 34 ms 33272 KB
1_052.txt AC 16 ms 30848 KB
1_053.txt AC 34 ms 28920 KB
1_054.txt AC 19 ms 27132 KB
1_055.txt AC 34 ms 33272 KB
1_056.txt AC 22 ms 27516 KB
1_057.txt AC 11 ms 30208 KB
1_058.txt AC 32 ms 33152 KB
1_059.txt AC 22 ms 27008 KB
1_060.txt AC 36 ms 28288 KB
1_061.txt AC 48 ms 35712 KB
1_062.txt AC 31 ms 33024 KB
1_063.txt AC 33 ms 32512 KB
1_064.txt AC 27 ms 31744 KB
1_065.txt AC 11 ms 26112 KB
1_066.txt AC 36 ms 28668 KB
1_067.txt AC 17 ms 30848 KB
1_068.txt AC 29 ms 32128 KB
1_069.txt AC 21 ms 31104 KB
1_070.txt AC 30 ms 31872 KB
1_071.txt AC 39 ms 28544 KB
1_072.txt AC 27 ms 27648 KB
1_073.txt AC 14 ms 30464 KB
1_074.txt AC 13 ms 30336 KB
1_075.txt AC 29 ms 31744 KB
1_076.txt AC 55 ms 35968 KB
1_077.txt AC 38 ms 28544 KB
1_078.txt AC 19 ms 26880 KB
1_079.txt AC 28 ms 31744 KB
1_080.txt AC 30 ms 32000 KB
1_081.txt AC 58 ms 39296 KB
1_082.txt AC 62 ms 40448 KB
1_083.txt AC 45 ms 29056 KB
1_084.txt AC 45 ms 29056 KB
1_085.txt AC 60 ms 42240 KB
1_086.txt AC 60 ms 41856 KB
1_087.txt AC 43 ms 36344 KB
1_088.txt AC 43 ms 36344 KB
1_089.txt AC 39 ms 29432 KB
1_090.txt AC 40 ms 29432 KB
1_091.txt AC 44 ms 36344 KB
1_092.txt AC 39 ms 29432 KB
1_093.txt AC 57 ms 37760 KB
1_094.txt AC 57 ms 38016 KB
1_095.txt AC 46 ms 29056 KB
1_096.txt AC 44 ms 29056 KB
1_097.txt AC 57 ms 37760 KB
1_098.txt AC 57 ms 37760 KB
1_099.txt AC 50 ms 36220 KB
1_100.txt AC 50 ms 36220 KB
1_101.txt AC 43 ms 29308 KB
1_102.txt AC 42 ms 29308 KB
1_103.txt AC 56 ms 36220 KB
1_104.txt AC 50 ms 36220 KB
1_105.txt AC 58 ms 36096 KB
1_106.txt AC 57 ms 36096 KB
1_107.txt AC 46 ms 29184 KB
1_108.txt AC 46 ms 29184 KB
1_109.txt AC 56 ms 36096 KB
1_110.txt AC 54 ms 36096 KB
1_111.txt AC 55 ms 36096 KB
1_112.txt AC 56 ms 36096 KB
1_113.txt AC 45 ms 29184 KB
1_114.txt AC 45 ms 29184 KB
1_115.txt AC 55 ms 36096 KB
1_116.txt AC 56 ms 36096 KB