Submission #280440

# Submission time Handle Problem Language Result Execution time Memory
280440 2020-08-22T19:05:08 Z Noam13 Toy Train (IOI17_train) C++14
27 / 100
16 ms 2176 KB
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define ii pair<int, int>
#define x first
#define y second
#define vii vector<ii>
#define pb push_back
#define all(x) x.begin(), x.end()
#define loop(i,s,e) for(int i=s;i<e;++i)
#define loopr(i,s,e) for(int i=e-1;i>=s;--i)
#define chkmin(a,b) a = min(a,b)
#define chkmax(a,b) a = max(a,b)
using namespace std;
const int INF = 1e9;


int n;
vb check;
void dfs(int cur, vi& vec, vvi& g){
    if (check[cur]) return ;
    check[cur] = 1; 
    for(auto nei:g[cur]) dfs(nei, vec, g);
    vec.pb(cur);
}
vvi g, ag;
vi a,r;
int dp[15][1<<15];
int tar;
int get(int i, int mask){
    int &v = dp[i][mask];
    if (v!=-1) return v;
    mask|=(1<<i);
    if (a[i]) {
        for(auto nei:g[i]){
            if (nei==tar) return v=1;
            if ((1<<nei)&mask) continue;
            if (get(nei, mask)) return v=1;
        }
        return v=0;
    }
    else{
        for(auto nei:g[i]){
            if (nei==tar) continue;
            if ((1<<nei)&mask) return v=0;
            if (!get(nei, mask)) return v=0;
        }
        return v=1;
    }
}
int checkS(int i){
    tar = i;
    loop(i,0,n) loop(j,0,(1<<n)) dp[i][j] = -1;
    return get(i,0);
}
vi solvesmall(){
    vi res(n);
    loop(i,0,n){
        if (r[i]){
            res[i] = checkS(i);
        }
    }
    loop(j,0,n*n){
        loop(i,0,n){
        if (a[i]){
            for(auto nei:g[i]){
                if (res[nei]) res[i] = 1;
            }
        }
        else{
            int cur = 1;
            for(auto nei:g[i]){
                if (!res[nei]) cur=0;
            }
            res[i] = cur;
        }
        }
    }
    return res;
}
vi who_wins(vi aa, vi rr, vi u, vi v) {
    a = aa, r = rr;
    n = a.size();
    g.resize(n); ag.resize(n); check.resize(n);
    loop(i,0,u.size()){
        int a = u[i], b = v[i];
        g[a].pb(b);
        ag[b].pb(a);
    }
    vi res(n);
    if (n<=15) return solvesmall();
    bool same = 1;
    loop(i,1,n) if(a[i]!=a[0]) same = 0;
    if (!same){
        res[n-1] = r[n-1];
        loopr(i,0,n-1){
            int tmp = -1;
            res[i] = r[i];
            for(auto nei:g[i]){
                if (nei==i){
                    if ((a[i] ^ r[i]) == 0) tmp = r[i];
                }
                else res[i] = res[nei];
            }
            if (tmp!=-1) res[i] = tmp;
        }
        return res;
    }
    if (a[0]){
        vi order;
        loop(i,0,n) dfs(i, order, ag);
        reverse(all(order)); 
        fill(all(check),0);
        for(auto cur:order){
            if (check[cur]) continue;
            vi vec;
            dfs(cur, vec, g);
            /*for(auto i:vec) cout<<i<<" ";
            cout<<endl;*/
            bool good = 0;
            for(auto i:vec) if (r[i]) good = 1;
            if (vec.size()==1){
                bool self = 0;
                for(auto nei:g[vec[0]]) if (nei==vec[0]) self = 1;
                if (!self) good = 0;
            }
            if (!good){
                for(auto c:vec) for(auto nei:g[c]){
                    if (res[nei]) good = 1;
                }
            }
            for(auto i:vec) res[i] = good;
        }
    }
    else{
        loop(i,0,n) check[i]=r[i];
        vi order;
        loop(i,0,n) dfs(i, order, ag);
        reverse(all(order)); 
        loop(i,0,n) check[i]=r[i];
        for(auto cur:order){
            if (check[cur]) continue;
            vi vec;
            dfs(cur, vec, g);
            bool good = 1;
            if (vec.size()==1){
                good = 0;
                for(auto nei:g[vec[0]]) if (nei==vec[0]) good = 1;
            }
            for(auto i:vec) res[i] = good;
        }
        order.clear();
        fill(all(check),0);
        loop(i,0,n) dfs(i, order, ag);
        reverse(all(order)); 
        fill(all(check),0);
        for(auto cur:order){
            if (check[cur]) continue;
            vi vec;
            dfs(cur, vec, g);
            /*for(auto i:vec) cout<<i<<" ";
            cout<<endl;*/
            bool good = 0;
            for(auto i:vec){
                if (res[i]) good = 1;
                else for(auto nei:g[i]) if(res[nei]) good = 1;
            }
            for(auto i:vec) res[i] = good;
        }
        loop(i,0,n) res[i] = !res[i];
    }
	return res;
}

/*
clear
g++ c.cpp grader.cpp -o c ; ./c
6 8
0 1 0 0 1 0
1 0 0 0 0 1
0 1
1 2
2 3
3 1
4 5
5 5
4 1
1 4
*/

Compilation message

train.cpp: In function 'std::vector<int> who_wins(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
train.cpp:12:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define loop(i,s,e) for(int i=s;i<e;++i)
......
   87 |     loop(i,0,u.size()){
      |          ~~~~~~~~~~~~             
train.cpp:87:5: note: in expansion of macro 'loop'
   87 |     loop(i,0,u.size()){
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1152 KB Output is correct
2 Correct 5 ms 1152 KB Output is correct
3 Correct 5 ms 1152 KB Output is correct
4 Correct 5 ms 1152 KB Output is correct
5 Correct 5 ms 1152 KB Output is correct
6 Correct 5 ms 1152 KB Output is correct
7 Correct 6 ms 1152 KB Output is correct
8 Correct 5 ms 1152 KB Output is correct
9 Correct 5 ms 1152 KB Output is correct
10 Correct 5 ms 1024 KB Output is correct
11 Correct 4 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 2176 KB 3rd lines differ - on the 2nd token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1792 KB Output is correct
2 Correct 9 ms 1792 KB Output is correct
3 Correct 9 ms 1792 KB Output is correct
4 Correct 11 ms 1664 KB Output is correct
5 Correct 11 ms 1536 KB Output is correct
6 Correct 11 ms 1536 KB Output is correct
7 Correct 11 ms 1536 KB Output is correct
8 Correct 10 ms 1536 KB Output is correct
9 Correct 9 ms 1536 KB Output is correct
10 Correct 10 ms 1408 KB Output is correct
11 Correct 10 ms 1408 KB Output is correct
12 Correct 9 ms 1408 KB Output is correct
13 Correct 10 ms 1664 KB Output is correct
14 Correct 16 ms 1664 KB Output is correct
15 Correct 10 ms 1664 KB Output is correct
16 Correct 15 ms 1664 KB Output is correct
17 Correct 10 ms 1664 KB Output is correct
18 Correct 10 ms 1408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 1408 KB Output is correct
2 Correct 13 ms 1408 KB Output is correct
3 Correct 11 ms 1440 KB Output is correct
4 Correct 11 ms 1408 KB Output is correct
5 Correct 11 ms 1536 KB Output is correct
6 Correct 11 ms 1536 KB Output is correct
7 Correct 11 ms 1408 KB Output is correct
8 Correct 10 ms 1408 KB Output is correct
9 Correct 12 ms 1408 KB Output is correct
10 Correct 10 ms 1664 KB Output is correct
11 Correct 10 ms 1664 KB Output is correct
12 Correct 13 ms 1664 KB Output is correct
13 Correct 11 ms 1536 KB Output is correct
14 Correct 10 ms 1408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 1408 KB 3rd lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1152 KB Output is correct
2 Correct 5 ms 1152 KB Output is correct
3 Correct 5 ms 1152 KB Output is correct
4 Correct 5 ms 1152 KB Output is correct
5 Correct 5 ms 1152 KB Output is correct
6 Correct 5 ms 1152 KB Output is correct
7 Correct 6 ms 1152 KB Output is correct
8 Correct 5 ms 1152 KB Output is correct
9 Correct 5 ms 1152 KB Output is correct
10 Correct 5 ms 1024 KB Output is correct
11 Correct 4 ms 1024 KB Output is correct
12 Incorrect 3 ms 2176 KB 3rd lines differ - on the 2nd token, expected: '1', found: '0'
13 Halted 0 ms 0 KB -