Submission #50972

# Submission time Handle Problem Language Result Execution time Memory
50972 2018-06-15T10:18:52 Z model_code None (JOI16_ho_t3) C++17
100 / 100
288 ms 17580 KB
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;

const int MAX_N = 100000;
const int MAX_M = 200000;

vector<int> G[MAX_N];
int dis[MAX_N];
int N;

int Q;

void bfs_all(int s){
	for(int i = 0; i < N; i++){
		dis[i] = -1;
	}
	dis[s] = 0;
	queue<int> que;
	que.push(s);
	while(!que.empty()){
		int v = que.front();
		que.pop();
		for(int i = 0; i < G[v].size(); ++i){
			int u = G[v][i];
			if(dis[u] != -1 && dis[u] <= dis[v] + 1) continue;
			dis[u] = dis[v] + 1;
			que.push(u);
		}
	}
}

bool ok[MAX_N];

vector<int> G2[MAX_N];

int bfs(int u, int v){
	if(dis[u] == dis[v]){
		return 0;
	}
	if(dis[u] > dis[v]) swap(u, v);
	G2[u].push_back(v);
	int res = 0;
	if(ok[u]){
		queue<int> que;
		que.push(v);
		while(!que.empty()){
			int v = que.front();
			que.pop();
			if(ok[v]) continue;
			ok[v] = true;
			res++;
			for(int i = 0; i < G2[v].size(); ++i){
				int u = G2[v][i];
				que.push(u);
			}
		}
	}
	return res;
}

int qs[MAX_M];
int us[MAX_M],vs[MAX_M];
int M;

int ord[MAX_M];
bool used[MAX_M];

void init(){
	for(int i = 0; i < M; ++i){
		used[i] = false;
	}
	for(int q = 0; q < Q; ++q){
		ord[q] = qs[q];
		used[qs[q]] = true;
	}
	int c = Q;
	for(int i = 0; i < M; ++i){
		if(!used[i]){
			ord[c++] = i;
		}
	}
}

int cnt[MAX_M + 1];

int main(){
	scanf("%d%d%d", &N, &M, &Q);
	for(int i = 0; i < M; ++i){
		scanf("%d%d", us + i, vs + i);
		us[i]--;
		vs[i]--;
		G[us[i]].push_back(vs[i]);
		G[vs[i]].push_back(us[i]);
	}
	bfs_all(0);
	for(int q = 0; q < Q; ++q){
		scanf("%d", qs + q);
		qs[q]--;
	}
	init();
	int prv = 0;
	ok[0] = true;
	cnt[M] = 0;
	for(int i = M - 1; i >= 0; --i){
		int u = us[ord[i]], v = vs[ord[i]];
		int cur = bfs(u, v);
		cnt[i] = prv + cur;
		prv = cnt[i];
	}
	for(int q = 0; q < Q; ++q){
		printf("%d\n", N - 1 - cnt[q + 1]);
	}
	return 0;
}

Compilation message

2016_ho_t3.cpp: In function 'void bfs_all(int)':
2016_ho_t3.cpp:27:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < G[v].size(); ++i){
                  ~~^~~~~~~~~~~~~
2016_ho_t3.cpp: In function 'int bfs(int, int)':
2016_ho_t3.cpp:56:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int i = 0; i < G2[v].size(); ++i){
                   ~~^~~~~~~~~~~~~~
2016_ho_t3.cpp: In function 'int main()':
2016_ho_t3.cpp:91:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d", &N, &M, &Q);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
2016_ho_t3.cpp:93:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", us + i, vs + i);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
2016_ho_t3.cpp:101:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", qs + q);
   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4988 KB Output is correct
2 Correct 6 ms 5224 KB Output is correct
3 Correct 7 ms 5424 KB Output is correct
4 Correct 6 ms 5424 KB Output is correct
5 Correct 6 ms 5424 KB Output is correct
6 Correct 6 ms 5424 KB Output is correct
7 Correct 5 ms 5424 KB Output is correct
8 Correct 6 ms 5424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4988 KB Output is correct
2 Correct 6 ms 5224 KB Output is correct
3 Correct 7 ms 5424 KB Output is correct
4 Correct 6 ms 5424 KB Output is correct
5 Correct 6 ms 5424 KB Output is correct
6 Correct 6 ms 5424 KB Output is correct
7 Correct 5 ms 5424 KB Output is correct
8 Correct 6 ms 5424 KB Output is correct
9 Correct 164 ms 14856 KB Output is correct
10 Correct 224 ms 14864 KB Output is correct
11 Correct 174 ms 15168 KB Output is correct
12 Correct 168 ms 15220 KB Output is correct
13 Correct 176 ms 15232 KB Output is correct
14 Correct 171 ms 15444 KB Output is correct
15 Correct 110 ms 15444 KB Output is correct
16 Correct 134 ms 15444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 229 ms 15444 KB Output is correct
2 Correct 227 ms 15444 KB Output is correct
3 Correct 179 ms 15444 KB Output is correct
4 Correct 215 ms 15444 KB Output is correct
5 Correct 151 ms 15444 KB Output is correct
6 Correct 193 ms 15444 KB Output is correct
7 Correct 114 ms 15444 KB Output is correct
8 Correct 125 ms 15444 KB Output is correct
9 Correct 74 ms 15444 KB Output is correct
10 Correct 87 ms 15444 KB Output is correct
11 Correct 205 ms 15632 KB Output is correct
12 Correct 186 ms 15632 KB Output is correct
13 Correct 175 ms 15632 KB Output is correct
14 Correct 195 ms 15684 KB Output is correct
15 Correct 195 ms 15716 KB Output is correct
16 Correct 193 ms 15716 KB Output is correct
17 Correct 176 ms 16036 KB Output is correct
18 Correct 136 ms 16036 KB Output is correct
19 Correct 214 ms 17440 KB Output is correct
20 Correct 168 ms 17440 KB Output is correct
21 Correct 125 ms 17440 KB Output is correct
22 Correct 110 ms 17440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4988 KB Output is correct
2 Correct 6 ms 5224 KB Output is correct
3 Correct 7 ms 5424 KB Output is correct
4 Correct 6 ms 5424 KB Output is correct
5 Correct 6 ms 5424 KB Output is correct
6 Correct 6 ms 5424 KB Output is correct
7 Correct 5 ms 5424 KB Output is correct
8 Correct 6 ms 5424 KB Output is correct
9 Correct 164 ms 14856 KB Output is correct
10 Correct 224 ms 14864 KB Output is correct
11 Correct 174 ms 15168 KB Output is correct
12 Correct 168 ms 15220 KB Output is correct
13 Correct 176 ms 15232 KB Output is correct
14 Correct 171 ms 15444 KB Output is correct
15 Correct 110 ms 15444 KB Output is correct
16 Correct 134 ms 15444 KB Output is correct
17 Correct 229 ms 15444 KB Output is correct
18 Correct 227 ms 15444 KB Output is correct
19 Correct 179 ms 15444 KB Output is correct
20 Correct 215 ms 15444 KB Output is correct
21 Correct 151 ms 15444 KB Output is correct
22 Correct 193 ms 15444 KB Output is correct
23 Correct 114 ms 15444 KB Output is correct
24 Correct 125 ms 15444 KB Output is correct
25 Correct 74 ms 15444 KB Output is correct
26 Correct 87 ms 15444 KB Output is correct
27 Correct 205 ms 15632 KB Output is correct
28 Correct 186 ms 15632 KB Output is correct
29 Correct 175 ms 15632 KB Output is correct
30 Correct 195 ms 15684 KB Output is correct
31 Correct 195 ms 15716 KB Output is correct
32 Correct 193 ms 15716 KB Output is correct
33 Correct 176 ms 16036 KB Output is correct
34 Correct 136 ms 16036 KB Output is correct
35 Correct 214 ms 17440 KB Output is correct
36 Correct 168 ms 17440 KB Output is correct
37 Correct 125 ms 17440 KB Output is correct
38 Correct 110 ms 17440 KB Output is correct
39 Correct 218 ms 17440 KB Output is correct
40 Correct 288 ms 17440 KB Output is correct
41 Correct 161 ms 17440 KB Output is correct
42 Correct 216 ms 17440 KB Output is correct
43 Correct 256 ms 17440 KB Output is correct
44 Correct 236 ms 17452 KB Output is correct
45 Correct 271 ms 17580 KB Output is correct
46 Correct 145 ms 17580 KB Output is correct
47 Correct 127 ms 17580 KB Output is correct
48 Correct 119 ms 17580 KB Output is correct
49 Correct 99 ms 17580 KB Output is correct
50 Correct 111 ms 17580 KB Output is correct
51 Correct 106 ms 17580 KB Output is correct
52 Correct 125 ms 17580 KB Output is correct
53 Correct 110 ms 17580 KB Output is correct
54 Correct 97 ms 17580 KB Output is correct