# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
778483 | 2023-07-10T11:05:59 Z | I_Love_EliskaM_ | Toy Train (IOI17_train) | C++14 | 0 ms | 0 KB |
#include "train.h" #include <bits/stdc++.h> using namespace std; #define forn(i,n) for(int i=0;i<n;++i) #define pb push_back #define ll long long vector<int> p1(vector<int>a,vector<int>r,vector<int>u,vector<int>v) { int n=a.size(), m=u.size(); vector<int> s(n); vector<int> f(n); vector<int> ans(n); forn(i,m) if (u[i]==v[i]) s[u[i]]=1; forn(i,m) if (u[i]!=v[i]) f[u[i]]=1; int now=0; for (int i=n-1; i>=0; --i) { if (s[i]) { if (now) { if (!a[i]) { if (!r[i]) now=0; } else { if (f[i]) now=1; else now = r[i]; } } else { if (a[i]) { if (r[i]) now=1; } else { if (f[i]) now=0; else now = r[i]; } } } ans[i]=now; } return ans; } const int N=5555; vector<int> adj[N]; int vis[N]; int ok[N]; int dfs(int u, int s) { if (vis[u] && u==s) return 1; if (vis[u]) return 0; vis[u]=1; int z=0; for(auto&v:adj[u]) z|=dfs(v,s); return z; } int dfs2(int u) { if (ok[u]) return 1; if (vis[u]) return 0; vis[u]=1; int z=0; for(auto&v:adj[u]) z|=dfs2(v); return z; } vector<int> p3(vector<int>a,vector<int>r,vector<int>u,vector<int>v) { int n=a.size(), m=u.size(); vector<int> ans(n); forn(i,m) adj[u[i]].pb(v[i]); forn(i,n) if (r[i]) { forn(j,n) vis[j]=0; int q=dfs(i,i); ok[i]=q; } forn(i,n) { forn(j,n) vis[j]=0; int q=dfs2(i); ans[i]=q; } return ans; }