답안 #45557

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
45557 2018-04-15T11:31:32 Z IvanC Shell (info1cup18_shell) C++17
100 / 100
569 ms 41260 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 10;
const ll MOD = 1e9 + 7;
vector<int> grafo[MAXN],especiais;
int nivel[MAXN],grau[MAXN],N,M,P,marcado[MAXN],nivelatual,alvo;
ll dp[MAXN];
ll solve(int v){
	if(nivel[v] > nivelatual) return 0;
	if(nivel[v] == nivelatual && v != alvo) return 0;
	if(dp[v] != -1) return dp[v];
	ll tot = 0;
	for(int u : grafo[v]){
		tot += solve(u);
	}
	return dp[v] = (tot % MOD);
}
int main(){
	memset(dp,-1,sizeof(dp));
	scanf("%d %d %d",&N,&M,&P);
	especiais.push_back(1);
	for(int i = 1;i<=P;i++){
		int v;
		scanf("%d",&v);
		if(v != 1) especiais.push_back(v);
	}
	if(especiais.back() != N) especiais.push_back(N);
	for(int i = 1;i<=M;i++){
		int u,v;
		scanf("%d %d",&u,&v);
		grafo[u].push_back(v);
		grau[v]++;
	}
	queue<int> bfs;
	for(int i = 1;i<=N;i++) if(grau[i] == 0) bfs.push(i);
	while(!bfs.empty()){
		int v = bfs.front();
		bfs.pop();
		for(int u : grafo[v]){
			grau[u]--;
			if(grau[u] == 0){
				nivel[u] = nivel[v] + 1;
				bfs.push(u);
			}
		}
	} 
	for(int i = 0;i+1<especiais.size();i++){
		if(nivel[especiais[i]] >= nivel[especiais[i+1]]){
			printf("0\n");
			return 0;
		}
	}
	dp[N] = 1;
	for(int i = especiais.size() - 2;i>=0;i--){
		alvo = especiais[i+1];
		nivelatual = nivel[alvo];
		solve(especiais[i]);
	}
	printf("%lld\n",dp[1]);
	return 0;
}

Compilation message

shell.cpp: In function 'int main()':
shell.cpp:48:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0;i+1<especiais.size();i++){
                ~~~^~~~~~~~~~~~~~~~~
shell.cpp:21:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d",&N,&M,&P);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~
shell.cpp:25:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&v);
   ~~~~~^~~~~~~~~
shell.cpp:31:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&u,&v);
   ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 31608 KB Output is correct
2 Correct 27 ms 31716 KB Output is correct
3 Correct 27 ms 31792 KB Output is correct
4 Correct 27 ms 31868 KB Output is correct
5 Correct 27 ms 31868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 31608 KB Output is correct
2 Correct 27 ms 31716 KB Output is correct
3 Correct 27 ms 31792 KB Output is correct
4 Correct 27 ms 31868 KB Output is correct
5 Correct 27 ms 31868 KB Output is correct
6 Correct 28 ms 31868 KB Output is correct
7 Correct 33 ms 32044 KB Output is correct
8 Correct 30 ms 32044 KB Output is correct
9 Correct 36 ms 32084 KB Output is correct
10 Correct 35 ms 32084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 32084 KB Output is correct
2 Correct 132 ms 34524 KB Output is correct
3 Correct 126 ms 34664 KB Output is correct
4 Correct 142 ms 34664 KB Output is correct
5 Correct 102 ms 34664 KB Output is correct
6 Correct 300 ms 38592 KB Output is correct
7 Correct 275 ms 38592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 31608 KB Output is correct
2 Correct 27 ms 31716 KB Output is correct
3 Correct 27 ms 31792 KB Output is correct
4 Correct 27 ms 31868 KB Output is correct
5 Correct 27 ms 31868 KB Output is correct
6 Correct 28 ms 31868 KB Output is correct
7 Correct 33 ms 32044 KB Output is correct
8 Correct 30 ms 32044 KB Output is correct
9 Correct 36 ms 32084 KB Output is correct
10 Correct 35 ms 32084 KB Output is correct
11 Correct 27 ms 32084 KB Output is correct
12 Correct 132 ms 34524 KB Output is correct
13 Correct 126 ms 34664 KB Output is correct
14 Correct 142 ms 34664 KB Output is correct
15 Correct 102 ms 34664 KB Output is correct
16 Correct 300 ms 38592 KB Output is correct
17 Correct 275 ms 38592 KB Output is correct
18 Correct 545 ms 41260 KB Output is correct
19 Correct 552 ms 41260 KB Output is correct
20 Correct 569 ms 41260 KB Output is correct
21 Correct 194 ms 41260 KB Output is correct
22 Correct 487 ms 41260 KB Output is correct