Submission #575121

# Submission time Handle Problem Language Result Execution time Memory
575121 2022-06-09T17:19:22 Z definitelynotmee Toy Train (IOI17_train) C++
27 / 100
988 ms 262144 KB
#include<bits/stdc++.h>
#include"train.h"
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T>
using matrix = vector<vector<T>>;

std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {
	int owned = accumulate(all(a),0);
    int n = a.size(), m = u.size();
    vector<int> resp(n);
    matrix<int> g(n), rev(n);
    bool flag = 1;
    for(int i = 0; i < m; i++){
        g[u[i]].push_back(v[i]);
        rev[v[i]].push_back(u[i]);
        flag&=v[i] == u[i] || v[i] == u[i]+1;
    }
    if(owned == n){
        for(int i = 0; i < n; i++){
            if(!r[i])
                continue;
            vector<int> reach(n), reachedby(n);
            auto dfs =[&](int id, auto dfs)->void{
                reach[id] = 1;
                for(int i : g[id])
                    if(!reach[i]) dfs(i,dfs);
            };
            auto rdfs =[&](int id, auto dfs)->void{
                reachedby[id] = 1;
                for(int i : rev[id])
                    if(!reachedby[i]) dfs(i,dfs);
            };
            for(int j : g[i])
                dfs(j,dfs);
            for(int j : rev[i])
                rdfs(j,rdfs);
            if(reach[i]){
                for(int j = 0; j < n; j++)
                    resp[j]|=reachedby[j];
            }
        }       
        
    } else if(owned == 0){
        for(int i = 0; i < n; i++){
            if(r[i])
                continue;
            vector<int> reach(n), reachedby(n);
            auto dfs =[&](int id, auto dfs)->void{
                reach[id] = 1;
                for(int i : g[id])
                    if(!reach[i] && !r[i]) dfs(i,dfs);
            };
            auto rdfs =[&](int id, auto dfs)->void{
                reachedby[id] = 1;
                for(int i : rev[id])
                    if(!reachedby[i]) dfs(i,dfs);
            };
            for(int j : g[i])
                if(!r[j])dfs(j,dfs);
            for(int j : rev[i])
                rdfs(j,rdfs);
            if(reach[i]){
                for(int j = 0; j < n; j++)
                    resp[j]|=reachedby[j];
            }
        } 
        for(int& i : resp)
            i^=1;
    } else if(flag){
        for(int i = n-1; i >= 0; i--){
            int t1 = 0, t2 = 0;
            for(int j : g[i]){
                if(j == i)
                    t1 = 1;
                else if(j == i+1)
                    t2 = 1;
            }
            if(a[i]){
                if(t2){
                    resp[i] = resp[i+1];
                }
                if(t1 && r[i])
                    resp[i] = 1;
            } else {
                resp[i] = 1;
                if(t2){
                    resp[i] = resp[i+1];
                }
                if(t1 && !r[i])
                    resp[i] = 0;
            }
        }
    } else {
        vector<int> power(16,1);
        for(int i = 1; i < 16; i++)
            power[i] = power[i-1]*3;
        matrix<int> can(power[n],vector<int>(n,-1));
        auto getbit =[&](int x, int id){
            return( x%(power[id]*3)-x%power[id])/power[id];
        };
        auto solve=[&](int id, int mask, auto solve)->int{
            if(can[mask][id] != -1){
                return can[mask][id];
            }
            int resp = a[id]^1;
            for(int i : g[id]){
                int newmask = mask;
                int state = getbit(mask,i);
                if(state == 1){
                    // << 'a' << '\n';
                    if(!a[id]){
                        resp = 0;
                        break;
                    }
                } else if(state == 2){
                    // << 'b' << '\n';
                    if(a[id]){
                        resp = 1;
                        break;
                    }
                } else {
                    if(r[i]){
                        for(int j = 0; j < n; j++){
                            if(getbit(newmask,j) == 1){
                                newmask+=power[j];
                            }
                        }
                        newmask+=power[i]*2;
                    } else {
                        newmask+=power[i];
                    }
                    // << newmask << '\n';
                    int cur = solve(i,newmask,solve);
                    if(cur == a[id]){
                        resp = cur;
                        break;
                    }
                }
            }
            can[mask][id] = resp;
            return resp;
        };
        for(int i = 0; i < n; i++){
            int mask = power[i]*(1+r[i]);
            resp[i] = solve(i,mask,solve);
        }
    }
	return resp;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1108 KB Output is correct
2 Correct 5 ms 1108 KB Output is correct
3 Correct 4 ms 1108 KB Output is correct
4 Correct 4 ms 1108 KB Output is correct
5 Correct 4 ms 1108 KB Output is correct
6 Correct 4 ms 1108 KB Output is correct
7 Correct 10 ms 1152 KB Output is correct
8 Correct 11 ms 1108 KB Output is correct
9 Correct 4 ms 1084 KB Output is correct
10 Correct 4 ms 980 KB Output is correct
11 Correct 5 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 163 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 50 ms 1768 KB Output is correct
2 Correct 94 ms 1752 KB Output is correct
3 Correct 129 ms 1748 KB Output is correct
4 Correct 512 ms 1660 KB Output is correct
5 Correct 104 ms 1620 KB Output is correct
6 Correct 81 ms 1596 KB Output is correct
7 Correct 502 ms 1600 KB Output is correct
8 Correct 8 ms 1596 KB Output is correct
9 Correct 7 ms 1492 KB Output is correct
10 Correct 11 ms 1552 KB Output is correct
11 Correct 7 ms 1440 KB Output is correct
12 Correct 7 ms 1492 KB Output is correct
13 Correct 8 ms 1620 KB Output is correct
14 Correct 8 ms 1620 KB Output is correct
15 Correct 8 ms 1696 KB Output is correct
16 Correct 8 ms 1588 KB Output is correct
17 Correct 8 ms 1620 KB Output is correct
18 Correct 299 ms 1340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 1380 KB Output is correct
2 Correct 271 ms 1468 KB Output is correct
3 Correct 472 ms 1588 KB Output is correct
4 Correct 302 ms 1588 KB Output is correct
5 Correct 684 ms 1596 KB Output is correct
6 Correct 594 ms 1576 KB Output is correct
7 Correct 587 ms 1620 KB Output is correct
8 Correct 376 ms 1540 KB Output is correct
9 Correct 8 ms 1492 KB Output is correct
10 Correct 231 ms 1588 KB Output is correct
11 Correct 94 ms 1600 KB Output is correct
12 Correct 245 ms 1740 KB Output is correct
13 Correct 988 ms 1636 KB Output is correct
14 Correct 558 ms 1628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 2900 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1108 KB Output is correct
2 Correct 5 ms 1108 KB Output is correct
3 Correct 4 ms 1108 KB Output is correct
4 Correct 4 ms 1108 KB Output is correct
5 Correct 4 ms 1108 KB Output is correct
6 Correct 4 ms 1108 KB Output is correct
7 Correct 10 ms 1152 KB Output is correct
8 Correct 11 ms 1108 KB Output is correct
9 Correct 4 ms 1084 KB Output is correct
10 Correct 4 ms 980 KB Output is correct
11 Correct 5 ms 980 KB Output is correct
12 Runtime error 163 ms 262144 KB Execution killed with signal 9
13 Halted 0 ms 0 KB -