답안 #110817

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
110817 2019-05-12T09:46:53 Z autumn_eel 우주 해적 (JOI14_space_pirate) C++14
47 / 100
1529 ms 37156 KB
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef pair<int,int>P;
typedef long long ll;

vector<int>E[3100];

int a[3100];
int ans[3100];

P vis[3100];
int cycle[3100];

P d[3100];
int d1[3100];
int dist[3100];

int id[3100][3100];

int cnt=0;

void dfs(int v,int k){
	vis[v]=P(cnt,k);
	if(vis[a[v]].first!=-1){
		if(vis[a[v]].second!=k||cycle[a[v]])return;
		cycle[a[v]]=cnt+1-vis[a[v]].first;
	}
	cnt++;
	dfs(a[v],k);
}

void bfs(queue<int>&que,int d[]){
	while(!que.empty()){
		int p=que.front();que.pop();
		for(int u:E[p]){
			if(d[u]==-1){
				d[u]=d[p]+1;
				que.push(u);
			}
		}
	}
}

int main(){
	int n;ll K;cin>>n>>K;
	if(n>3000)abort();
	rep(i,n){
		scanf("%d",&a[i]);a[i]--;
		E[a[i]].push_back(i);
	}
	rep(i,n){
		int x=i;
		rep(j,n+1){
			id[i][j]=x;
			x=a[x];
		}
	}
	memset(vis,-1,sizeof(vis));
	rep(i,n){
		vis[i]=P(-1,-1);
	}
	int c1=0;
	rep(i,n){
		if(vis[i].first==-1){
			dfs(i,c1++);
		}
	}
	queue<int>que;
	rep(i,n)d[i]=P(-1,i);
	rep(i,n){
		if(cycle[i]){
			d[i]=P(0,i);
			que.push(i);
		}
	}
	while(!que.empty()){
		int p=que.front();que.pop();
		for(int u:E[p]){
			if(d[u].first==-1){
				d[u]=P(d[p].first+1,d[p].second);
				que.push(u);
			}
		}
	}
	memset(d1,-1,sizeof(d1));
	int x=0;
	d1[0]=0;
	while(1){
		if(d1[a[x]]!=-1)break;
		d1[a[x]]=d1[x]+1;
		x=a[x];
	}
	rep(i,n){
		if(d1[i]>=K||d1[i]==-1){
			if(K<d[0].first)ans[id[0][K]]+=n;
			else{
				ans[id[d[0].second][(K-d[0].first)%cycle[d[0].second]]]+=n;
			}
			continue;
		}
		memset(dist,-1,sizeof(dist));
		dist[i]=0;
		que.push(i);
		bfs(que,dist);
		ll b=K-d1[i];
		rep(j,n){
			if(dist[j]!=-1){
				int cy=dist[j]+1;
				if(b%cy==0){
					ans[i]++;
				}
				else ans[id[j][b%cy-1]]++;
			}
			else{
				if(b<=d[j].first)ans[id[j][b-1]]++;
				else{
					ans[id[d[j].second][(b-d[j].first-1)%cycle[d[j].second]]]++;
				}
			}
		}
	}
	rep(i,n){
		printf("%d\n",ans[i]);
	}
}

Compilation message

space_pirate.cpp: In function 'int main()':
space_pirate.cpp:49:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&a[i]);a[i]--;
   ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 896 KB Output is correct
2 Correct 4 ms 896 KB Output is correct
3 Correct 3 ms 896 KB Output is correct
4 Correct 3 ms 896 KB Output is correct
5 Correct 4 ms 896 KB Output is correct
6 Correct 3 ms 896 KB Output is correct
7 Correct 4 ms 896 KB Output is correct
8 Correct 4 ms 896 KB Output is correct
9 Correct 2 ms 1024 KB Output is correct
10 Correct 2 ms 896 KB Output is correct
11 Correct 3 ms 896 KB Output is correct
12 Correct 2 ms 896 KB Output is correct
13 Correct 3 ms 896 KB Output is correct
14 Correct 2 ms 896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 896 KB Output is correct
2 Correct 4 ms 896 KB Output is correct
3 Correct 3 ms 896 KB Output is correct
4 Correct 3 ms 896 KB Output is correct
5 Correct 4 ms 896 KB Output is correct
6 Correct 3 ms 896 KB Output is correct
7 Correct 4 ms 896 KB Output is correct
8 Correct 4 ms 896 KB Output is correct
9 Correct 2 ms 1024 KB Output is correct
10 Correct 2 ms 896 KB Output is correct
11 Correct 3 ms 896 KB Output is correct
12 Correct 2 ms 896 KB Output is correct
13 Correct 3 ms 896 KB Output is correct
14 Correct 2 ms 896 KB Output is correct
15 Correct 60 ms 37132 KB Output is correct
16 Correct 47 ms 36896 KB Output is correct
17 Correct 61 ms 36984 KB Output is correct
18 Correct 1529 ms 37088 KB Output is correct
19 Correct 687 ms 36984 KB Output is correct
20 Correct 754 ms 37156 KB Output is correct
21 Correct 970 ms 37112 KB Output is correct
22 Correct 348 ms 37052 KB Output is correct
23 Correct 866 ms 37112 KB Output is correct
24 Correct 217 ms 37112 KB Output is correct
25 Correct 46 ms 36984 KB Output is correct
26 Correct 1456 ms 37112 KB Output is correct
27 Correct 290 ms 37112 KB Output is correct
28 Correct 505 ms 37112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 896 KB Output is correct
2 Correct 4 ms 896 KB Output is correct
3 Correct 3 ms 896 KB Output is correct
4 Correct 3 ms 896 KB Output is correct
5 Correct 4 ms 896 KB Output is correct
6 Correct 3 ms 896 KB Output is correct
7 Correct 4 ms 896 KB Output is correct
8 Correct 4 ms 896 KB Output is correct
9 Correct 2 ms 1024 KB Output is correct
10 Correct 2 ms 896 KB Output is correct
11 Correct 3 ms 896 KB Output is correct
12 Correct 2 ms 896 KB Output is correct
13 Correct 3 ms 896 KB Output is correct
14 Correct 2 ms 896 KB Output is correct
15 Correct 60 ms 37132 KB Output is correct
16 Correct 47 ms 36896 KB Output is correct
17 Correct 61 ms 36984 KB Output is correct
18 Correct 1529 ms 37088 KB Output is correct
19 Correct 687 ms 36984 KB Output is correct
20 Correct 754 ms 37156 KB Output is correct
21 Correct 970 ms 37112 KB Output is correct
22 Correct 348 ms 37052 KB Output is correct
23 Correct 866 ms 37112 KB Output is correct
24 Correct 217 ms 37112 KB Output is correct
25 Correct 46 ms 36984 KB Output is correct
26 Correct 1456 ms 37112 KB Output is correct
27 Correct 290 ms 37112 KB Output is correct
28 Correct 505 ms 37112 KB Output is correct
29 Runtime error 3 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
30 Halted 0 ms 0 KB -