답안 #575481

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
575481 2022-06-10T18:48:14 Z definitelynotmee 장난감 기차 (IOI17_train) C++
49 / 100
1000 ms 224968 KB
#include<bits/stdc++.h>
#include"train.h"
#define mp make_pair
#define mt make_tuple
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
using namespace std;
template <typename T>
using matrix = vector<vector<T>>;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INFL = (1LL<<62)-1;
const int INF = (1<<30)-1;
const double EPS = 1e-7;
const int MOD = 1e9 + 7;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
const int MAXN = 1e6+1;


std::vector<int> whowins1cara(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {

	int n = a.size(), m = u.size();
	matrix<int> g(n), rev(n);
	for(int i = 0; i < m; i++){
		g[u[i]].push_back(v[i]);
		rev[v[i]].push_back(u[i]);
	}
	vector<int> resp(n);
	vector<int> degree(n);
	for(int i = 0; i < n; i++)
		degree[i] = g[i].size();
	for(int v = 0; v < n; v++){
		if(!r[v])
			continue;

		vector<int> win = resp, grau = degree;
		auto processwin = [&](int id, auto f)->void{
			for(int i : rev[id]){
				if(grau[i] == 0)
					continue;
				if(a[i] == 1){
					grau[i] = 0;
				} else grau[i]--;
				if(grau[i] == 0){
					win[i] = 1;
					if(i != v)
						f(i,f);
				}
			}
		};

		if(!win[v])
			processwin(v,processwin);
		if(win[v]){
			swap(resp,win);
			swap(grau,degree);
		}
	}

	return resp;
}

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 recharge = accumulate(all(r),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 if(recharge == 1){
		return whowins1cara(a,r,u,v);
	} else {
        vector<int> power(16,1);
        for(int i = 1; i < 16; i++)
            power[i] = power[i-1]*3;
        matrix<char> can(n,vector<char>(power[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[id][mask] != -1){
                return can[id][mask];
            }
            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[id][mask] = 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 980 KB Output is correct
2 Correct 6 ms 980 KB Output is correct
3 Correct 5 ms 980 KB Output is correct
4 Correct 4 ms 980 KB Output is correct
5 Correct 4 ms 980 KB Output is correct
6 Correct 4 ms 980 KB Output is correct
7 Correct 11 ms 1076 KB Output is correct
8 Correct 10 ms 1076 KB Output is correct
9 Correct 4 ms 980 KB Output is correct
10 Correct 4 ms 980 KB Output is correct
11 Correct 3 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 224864 KB Output is correct
2 Correct 94 ms 224944 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 93 ms 224884 KB Output is correct
5 Correct 98 ms 224932 KB Output is correct
6 Correct 91 ms 224916 KB Output is correct
7 Correct 96 ms 224952 KB Output is correct
8 Correct 91 ms 224868 KB Output is correct
9 Correct 96 ms 224968 KB Output is correct
10 Correct 90 ms 224864 KB Output is correct
11 Correct 91 ms 224940 KB Output is correct
12 Correct 93 ms 224864 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 99 ms 224912 KB Output is correct
19 Correct 92 ms 224884 KB Output is correct
20 Correct 93 ms 224944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 1492 KB Output is correct
2 Correct 91 ms 1544 KB Output is correct
3 Correct 136 ms 1492 KB Output is correct
4 Correct 524 ms 1456 KB Output is correct
5 Correct 94 ms 1428 KB Output is correct
6 Correct 81 ms 1396 KB Output is correct
7 Correct 498 ms 1364 KB Output is correct
8 Correct 7 ms 1364 KB Output is correct
9 Correct 6 ms 1364 KB Output is correct
10 Correct 10 ms 1364 KB Output is correct
11 Correct 7 ms 1348 KB Output is correct
12 Correct 6 ms 1236 KB Output is correct
13 Correct 8 ms 1480 KB Output is correct
14 Correct 7 ms 1364 KB Output is correct
15 Correct 7 ms 1492 KB Output is correct
16 Correct 8 ms 1468 KB Output is correct
17 Correct 8 ms 1476 KB Output is correct
18 Correct 310 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 1272 KB Output is correct
2 Correct 264 ms 1236 KB Output is correct
3 Correct 474 ms 1488 KB Output is correct
4 Correct 285 ms 1376 KB Output is correct
5 Correct 667 ms 1384 KB Output is correct
6 Correct 592 ms 1384 KB Output is correct
7 Correct 579 ms 1372 KB Output is correct
8 Correct 382 ms 1236 KB Output is correct
9 Correct 8 ms 1236 KB Output is correct
10 Correct 224 ms 1460 KB Output is correct
11 Correct 96 ms 1460 KB Output is correct
12 Correct 246 ms 1460 KB Output is correct
13 Correct 1000 ms 1444 KB Output is correct
14 Correct 575 ms 1344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 2260 KB Output is correct
2 Correct 10 ms 2260 KB Output is correct
3 Correct 10 ms 2288 KB Output is correct
4 Correct 9 ms 2132 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 5 ms 1620 KB Output is correct
7 Correct 6 ms 1236 KB Output is correct
8 Correct 6 ms 1364 KB Output is correct
9 Correct 5 ms 1296 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 5 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 980 KB Output is correct
2 Correct 6 ms 980 KB Output is correct
3 Correct 5 ms 980 KB Output is correct
4 Correct 4 ms 980 KB Output is correct
5 Correct 4 ms 980 KB Output is correct
6 Correct 4 ms 980 KB Output is correct
7 Correct 11 ms 1076 KB Output is correct
8 Correct 10 ms 1076 KB Output is correct
9 Correct 4 ms 980 KB Output is correct
10 Correct 4 ms 980 KB Output is correct
11 Correct 3 ms 980 KB Output is correct
12 Correct 126 ms 224864 KB Output is correct
13 Correct 94 ms 224944 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 93 ms 224884 KB Output is correct
16 Correct 98 ms 224932 KB Output is correct
17 Correct 91 ms 224916 KB Output is correct
18 Correct 96 ms 224952 KB Output is correct
19 Correct 91 ms 224868 KB Output is correct
20 Correct 96 ms 224968 KB Output is correct
21 Correct 90 ms 224864 KB Output is correct
22 Correct 91 ms 224940 KB Output is correct
23 Correct 93 ms 224864 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 99 ms 224912 KB Output is correct
30 Correct 92 ms 224884 KB Output is correct
31 Correct 93 ms 224944 KB Output is correct
32 Correct 47 ms 1492 KB Output is correct
33 Correct 91 ms 1544 KB Output is correct
34 Correct 136 ms 1492 KB Output is correct
35 Correct 524 ms 1456 KB Output is correct
36 Correct 94 ms 1428 KB Output is correct
37 Correct 81 ms 1396 KB Output is correct
38 Correct 498 ms 1364 KB Output is correct
39 Correct 7 ms 1364 KB Output is correct
40 Correct 6 ms 1364 KB Output is correct
41 Correct 10 ms 1364 KB Output is correct
42 Correct 7 ms 1348 KB Output is correct
43 Correct 6 ms 1236 KB Output is correct
44 Correct 8 ms 1480 KB Output is correct
45 Correct 7 ms 1364 KB Output is correct
46 Correct 7 ms 1492 KB Output is correct
47 Correct 8 ms 1468 KB Output is correct
48 Correct 8 ms 1476 KB Output is correct
49 Correct 310 ms 1108 KB Output is correct
50 Correct 40 ms 1272 KB Output is correct
51 Correct 264 ms 1236 KB Output is correct
52 Correct 474 ms 1488 KB Output is correct
53 Correct 285 ms 1376 KB Output is correct
54 Correct 667 ms 1384 KB Output is correct
55 Correct 592 ms 1384 KB Output is correct
56 Correct 579 ms 1372 KB Output is correct
57 Correct 382 ms 1236 KB Output is correct
58 Correct 8 ms 1236 KB Output is correct
59 Correct 224 ms 1460 KB Output is correct
60 Correct 96 ms 1460 KB Output is correct
61 Correct 246 ms 1460 KB Output is correct
62 Correct 1000 ms 1444 KB Output is correct
63 Correct 575 ms 1344 KB Output is correct
64 Correct 9 ms 2260 KB Output is correct
65 Correct 10 ms 2260 KB Output is correct
66 Correct 10 ms 2288 KB Output is correct
67 Correct 9 ms 2132 KB Output is correct
68 Correct 1 ms 340 KB Output is correct
69 Correct 5 ms 1620 KB Output is correct
70 Correct 6 ms 1236 KB Output is correct
71 Correct 6 ms 1364 KB Output is correct
72 Correct 5 ms 1296 KB Output is correct
73 Correct 2 ms 596 KB Output is correct
74 Correct 5 ms 1236 KB Output is correct
75 Runtime error 7 ms 4308 KB Execution killed with signal 11
76 Halted 0 ms 0 KB -