#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <cassert>
#include <algorithm>
using namespace std;
void printAdjMatrix(vector<vector<int>>& adjM) {
int n = adjM.size();
for(int i=0; i<n; i++) {
printf("%d:",i);
for(int elem: adjM[i])
printf(" %d",elem);
printf("\n");
}
}
void printAdjList(vector<vector<int>>& adjL) {
int n = adjL.size();
for(int i=0; i<n; i++) {
printf("%d:",i);
for(int elem: adjL[i])
printf(" %d", elem);
printf("\n");
}
}
void printDegree(vector<int>& Deg) {
int n = Deg.size();
for(int i=0; i<n; i++) printf("Deg[%d]=%d ",i,Deg[i]);
printf("\n");
}
bool isSubtask1(vector<int>& u, vector<int>& v) {
int m = u.size();
for(int i=0; i<m; i++)
if (v[i]!=u[i] && v[i]!=u[i]+1) return false;
return true;
}
bool isSubtask2(const int n) {return n <= 15;}
bool isSubtask3(const vector<int>& a) {return find(a.begin(),a.end(),0)==a.end();}
bool isSubtask4(const vector<int>& a) {return find(a.begin(),a.end(),1)==a.end();}
bool dfs(int now, int dest, vector<int>& a, vector<int>& r, vector<vector<int>>& adjL, vector<int>& dis, vector<int>& ans) {
// printf("now:%d, dest:%d, dis[%d]=%d, r[%d]=%d, ans[%d]=%d\n",now,dest,now,(int) dis[now],now,r[now],now,ans[now]);
if (now==dest) return true;
if (dis[now]) return false;
else dis[now] = 1;
if (a[now]) {
for(int i=0; i<adjL[now].size(); i++)
if(dfs(adjL[now][i],dest,a,r,adjL,dis,ans)) {ans[now]=1; break;}
}
else {
int i;
for(i=0; i<adjL[now].size(); i++)
if(!dfs(adjL[now][i],dest,a,r,adjL,dis,ans)) break;
if (i==adjL[now].size()) ans[now] = 1;
}
if (ans[now]) printf("win %d \n",now);
return ans[now];
}
void checkNeighbor(int now, vector<int>& a, vector<vector<int>>& adjL, vector<int>& ans) {
if (a[now]) {
for(int i=0; i<adjL[now].size(); i++)
if(ans[adjL[now][i]]) ans[now]=1;
}
else {
int i;
for(i=0; i<adjL[now].size(); i++)
if(!ans[adjL[now][i]]) break;
if (i==adjL[now].size()) ans[now] = 1;
}
}
bool dfs3(const int now, const int dest, const vector<vector<int>>& adjL, vector<int>& visit) {
if (visit[now]) return false;
visit[now] = 1;
if (now==dest) return true;
for(int i=0; i<adjL[now].size(); i++)
if(dfs3(adjL[now][i],dest,adjL,visit)) return true;
return false;
}
vector<int> fulltask(vector<int>& a, vector<int>& r, vector<int>& u, vector<int>& v) {
int n=a.size(), m=u.size();
vector<vector<int>> adjL(n+1), adjLRev(n+1);
for(int i=0; i<m; i++) {adjL[u[i]].push_back(v[i]); adjLRev[v[i]].push_back(u[i]);}
vector<int> ans(n), cnt(n);
queue<int> q;
bool update=true;
while(update) {
// for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) if (r[j]==1) q.push(j);
cnt.assign(n,0); ans.assign(n,0);
while(!q.empty()) {
int node = q.front(); q.pop();
for(int elem: adjLRev[node]) {
cnt[elem]++;
if (a[elem] && cnt[elem]==1 || !a[elem] && cnt[elem]==adjL[elem].size()) {
ans[elem]=1;
if(!r[elem]) q.push(elem);
}
}
}
// bool update=false;
update = false;
for(int j=0; j<n; j++) if(r[j]&&!ans[j]) {r[j] = 0; update=true;}
if (!update) return ans;
}
}
vector<int> subtask4(vector<int>& a, vector<int>& r, vector<int>& u, vector<int>& v) {
int n=a.size(), m=u.size();
vector<vector<int>> adjL(n+1), adjLRev(n+1);
for(int i=0; i<m; i++) {adjL[u[i]].push_back(v[i]); adjLRev[v[i]].push_back(u[i]);}
vector<int> cnt(n), ans(n);
queue<int> q;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) if (r[j]==1) q.push(j);
cnt.assign(n,0); ans.assign(n,0);
while(!q.empty()) {
int node = q.front(); q.pop();
for(int elem: adjLRev[node])
if (++cnt[elem]==adjL[elem].size()) {
ans[elem]=1;
if(!r[elem]) q.push(elem);
}
}
bool update=false;
for(int j=0; j<n; j++) if(r[j]&&!ans[j]) {r[j] = 0; update=true;}
if (!update) return ans;
}
}
vector<int> subtask3(vector<int>& a, vector<int>& r, vector<int>& u, vector<int>& v) {
int n=a.size(), m=u.size();
vector<vector<int>> adjL(n+1), adjLRev(n);
for(int i=0; i<m; i++) {adjL[u[i]].push_back(v[i]); adjLRev[v[i]].push_back(u[i]);}
vector<int> ans(n), visit(n+1);
for(int i=0; i<n; i++) {
if(r[i]) {
visit.assign(n+1,0);
adjL[n] = adjL[i];
if (dfs3(n,i,adjL,visit)) ans[i]=1;
}
}
// convert to dfs recursion (?)
queue<int> q;
for(int i=0; i<n; i++) if(ans[i]) q.push(i);
while(!q.empty()) {
int node = q.front(); q.pop();
for(int elem: adjLRev[node])
if (ans[elem]==0) {ans[elem]=1; q.push(elem);}
}
return ans;
}
vector<int> subtask2(vector<int>& a, vector<int>& r, vector<int>& u, vector<int>& v) {
int n=a.size(), m=u.size();
// queue<int> q;
vector<vector<int>> adjL(n), adjLRev(n), adjM(n,vector<int>(n));
vector<int> inDeg(n), outDeg(n);
for(int i=0; i<m; i++) {
adjL[u[i]].push_back(v[i]); adjLRev[v[i]].push_back(u[i]);
adjM[u[i]][v[i]] = 1;
inDeg[v[i]]++; outDeg[u[i]]++;
}
for(auto& list: adjL) sort(list.begin(),list.end());
for(auto& list: adjLRev) sort(list.begin(),list.end());
// printf("Adjacency Matrix\n"); printAdjMatrix(adjM);
// printf("Adjacency List\n"); printAdjList(adjL);
// printf("Adjacency List Reverse\n"); printAdjList(adjLRev);
// printf("outDegree\n"); printDegree(outDeg);
// printf("inDegree\n"); printDegree(inDeg);
vector<int> ans(n);
for(int i=0; i<n; i++)
if(r[i] && !ans[i]) {
vector<int> dis(n); int j;
dis[i] = 1;
if(a[i]) {
for(j=0; j<adjL[i].size(); j++)
if(dfs(adjL[i][j],i,a,r,adjL,dis,ans)) break;
if (j<adjL[i].size()) {ans[i] = 1;} // printf("ans[%d]=%d\n",i,ans[i]);}
}
else {
for(j=0; j<adjL[i].size(); j++)
if(!dfs(adjL[i][j],i,a,r,adjL,dis,ans)) break;
if (j==adjL[i].size()) {ans[i] = 1;} // printf("ans[%d]=%d\n",i,ans[i]);}
}
// if (ans[i]) printf("win %d \n",i);
}
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
if (ans[j]==0) checkNeighbor(j,a,adjL,ans);
return ans;
}
vector<int> subtask1(vector<int>& a, vector<int>& r, vector<int>& u, vector<int>& v) {
int n = a.size(), m = u.size();
vector<int> ans(n);
vector<vector<int>> adj(n,vector<int>(n));
for(int i=0; i<m; i++) adj[u[i]][v[i]] = 1;
ans[n-1]=r[n-1];
for(int i=n-2; i>=0; i--)
if (a[i]) {ans[i] = adj[i][i] && r[i] || adj[i][i+1] && ans[i+1];}
else {ans[i] = (adj[i][i]==0 || r[i]) && (adj[i][i+1]==0 || ans[i+1]);}
return ans;
}
vector<int> who_wins(vector<int> a, vector<int> r, vector<int> u, vector<int> v) {
return fulltask(a,r,u,v);
if (isSubtask4(a)) return subtask4(a,r,u,v);
if (isSubtask3(a)) return subtask3(a,r,u,v);
if (isSubtask1(u,v)) return subtask1(a,r,u,v);
return subtask1(a,r,u,v);
int n = a.size(), m = u.size();
vector<vector<int>> adj(n);
for(int i=0; i<m; i++) adj[u[i]].push_back(v[i]);
for(int i=0; i<n; i++) sort(adj[i].begin(),adj[i].end());
// if (isSubtask2(n)) return subtask2(a,r,u,v);
// for(int i=0; i<n; i++) {
// cout << i << ": ";
// for (const int elem: adj[i])
// cout << elem << " ";
// cout << endl;
// }
}
컴파일 시 표준 에러 (stderr) 메시지
train.cpp: In function 'std::vector<int> fulltask(std::vector<int>&, std::vector<int>&, std::vector<int>&, std::vector<int>&)':
train.cpp:109:1: warning: control reaches end of non-void function [-Wreturn-type]
109 | }
| ^
train.cpp: In function 'std::vector<int> subtask4(std::vector<int>&, std::vector<int>&, std::vector<int>&, std::vector<int>&)':
train.cpp:134:1: warning: control reaches end of non-void function [-Wreturn-type]
134 | }
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |