답안 #800636

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
800636 2023-08-01T17:11:17 Z Antekb 열대 식물원 (Tropical Garden) (IOI11_garden) C++17
100 / 100
116 ms 30036 KB
#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
using pii = pair<int, int>;
using vi = vector<int>;

void count_routes(int n, int m, int p, int edg[][2], int q, int QQ[])
{
	vector<pii> E[n][2];
	int naj[n][2], co[n][2], dist[n][2], typ[n][2];
	for(int i=0; i<n; i++){
		naj[i][0]=naj[i][1]=1e9+5;
	}
	for(int i=m-1; i>=0; i--){
		naj[edg[i][0]][0]=i;
		co[edg[i][0]][0]=edg[i][1];
		naj[edg[i][1]][0]=i;
		co[edg[i][1]][0]=edg[i][0];
	}
	for(int i=m-1; i>=0; i--){
		if(naj[edg[i][0]][0]!=i){
			naj[edg[i][0]][1]=i;
			co[edg[i][0]][1]=edg[i][1];
		}
		if(naj[edg[i][1]][0]!=i){
			naj[edg[i][1]][1]=i;
			co[edg[i][1]][1]=edg[i][0];
		}
	}
	for(int i=0; i<n; i++){
		dist[i][0]=dist[i][1]=1e9+5;
		if(naj[i][1]==1e9+5){
			naj[i][1]=naj[i][0];
			co[i][1]=co[i][0];
		}
		//cerr<<i<<" "<<naj[i][0]<<" "<<co[i][0]<<" "<<naj[i][1]<<" "<<co[i][1]<<"\n";
		E[co[i][0]][(naj[i][0]==naj[co[i][0]][0])].pb({i, 0});
		E[co[i][1]][(naj[i][1]==naj[co[i][1]][0])].pb({i, 1});
	}
	vector<pii> V;
	for(int i=0; i<n; i++){
		if(co[i][0]==p){
			V.pb({i, 0});
			dist[i][0]=1;
			typ[i][0]=(naj[i][0]==naj[p][0]);
		}
		if(co[i][1]==p){
			V.pb({i, 1});
			dist[i][1]=1;
			typ[i][1]=(naj[i][1]==naj[p][0]);
		}
	}
	for(int i=0; i<n; i++){
		for(int j=0; j<2; j++){
			//cerr<<i<<" "<<j<<": ";
			for(pii e:E[i][j]){
				//cerr<<"{"<<e.st<<", "<<e.nd<<"} ";
			}
			//cerr<<"\n";
		}
	}
	for(int i=0; i<V.size(); i++){
		//cerr<<V[i].st<<" "<<V[i].nd<<"\n";
		for(pii e:E[V[i].st][V[i].nd]){
			if(dist[e.st][e.nd]!=1e9+5)continue;
			V.pb(e);
			typ[e.st][e.nd]=typ[V[i].st][V[i].nd];
			dist[e.st][e.nd]=dist[V[i].st][V[i].nd]+1;
		}
	}
	vector<vector<int> > cykle;
	vector<pair<int, int> > eve;
	vector<int> eve2;
	for(int i=0; i<n; i++){
		for(int j=0; j<1; j++){
			if(dist[i][j]!=1e9+5){
				//cerr<<i<<" "<<j<<" "<<dist[i][j]<<" "<<typ[i][j]<<": ";
				if(dist[p][typ[i][j]]==1e9+5){
					eve2.pb(dist[i][j]);
					//cerr<<dist[i][j]<<"\n";
				}
				else{
					if(typ[p][typ[i][j]]==typ[i][j]){
						eve.pb({dist[i][j], dist[p][typ[i][j]]});
						//cerr<<"{"<<dist[i][j]<<", "<<dist[p][typ[i][j]]<<"}\n";
					}
					else{
						if(dist[p][typ[i][j]^1]==1e9+5){
							eve2.pb(dist[i][j]);
							eve2.pb(dist[i][j]+dist[p][typ[i][j]]);
							//cerr<<dist[i][j]<<" "<<dist[i][j]+dist[p][typ[i][j]]<<"\n";
						}
						else if(typ[p][typ[i][j]^1]==typ[i][j]){
							eve.pb({dist[i][j], dist[p][0]+dist[p][1]});
							eve.pb({dist[i][j]+dist[p][typ[i][j]], dist[p][0]+dist[p][1]});
							//cerr<<"{"<<dist[i][j]<<", "<<dist[p][0]+dist[p][1]<<"}";
							//cerr<<"{"<<dist[i][j]+dist[p][typ[i][j]]<<", "<<dist[p][0]+dist[p][1]<<"}\n";
						}
						else{
							eve2.pb(dist[i][j]);
							eve.pb({dist[i][j]+dist[p][typ[i][j]], dist[p][typ[i][j]^1]});
							//cerr<<"{"<<dist[i][j]<<", "<<dist[p][0]+dist[p][1]<<"}";
							//cerr<<"{"<<dist[i][j]+dist[p][typ[i][j]]<<", "<<dist[p][0]+dist[p][1]<<"}\n";
						
						}
					}
				}
			}
		}
	}
	sort(eve.begin(), eve.end());
	sort(eve2.begin(), eve2.end());
	reverse(eve.begin(), eve.end());
	reverse(eve2.begin(), eve2.end());
	vector<pii> Q(q);
	vector<int> ans(q);
	for(int i=0; i<q; i++){
		Q[i].st=QQ[i];
		Q[i].nd=i;
	}
	sort(Q.begin(), Q.end());
	for(pii ii:Q){
		int i=ii.st, qq=ii.nd;
		while(eve2.size() && eve2.back()<i)eve2.pop_back();
		while(eve2.size() && eve2.back()==i)eve2.pop_back(), ans[qq]++;
		while(eve.size() && eve.back().st<=i){
			if(cykle.size()>0 && cykle[0].size()==eve.back().nd){
				cykle[0][eve.back().st%eve.back().nd]++;
			}
			else if(cykle.size()>1 && cykle[1].size()==eve.back().nd){
				cykle[1][eve.back().st%eve.back().nd]++;
			}
			else{
				cykle.pb(vi(eve.back().nd));
				cykle.back()[eve.back().st%eve.back().nd]++;
			}
			eve.pop_back();
		}
		if(cykle.size()>0)ans[qq]+=cykle[0][i%cykle[0].size()];
		if(cykle.size()>1)ans[qq]+=cykle[1][i%cykle[1].size()];
	}
	for(int i=0; i<q; i++){
		answer(ans[i]);
	}
}


Compilation message

garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:60:12: warning: variable 'e' set but not used [-Wunused-but-set-variable]
   60 |    for(pii e:E[i][j]){
      |            ^
garden.cpp:66:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |  for(int i=0; i<V.size(); i++){
      |               ~^~~~~~~~~
garden.cpp:131:40: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  131 |    if(cykle.size()>0 && cykle[0].size()==eve.back().nd){
      |                                        ^
garden.cpp:134:45: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  134 |    else if(cykle.size()>1 && cykle[1].size()==eve.back().nd){
      |                                             ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 436 KB Output is correct
9 Correct 2 ms 576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 436 KB Output is correct
9 Correct 2 ms 576 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
11 Correct 9 ms 3540 KB Output is correct
12 Correct 16 ms 5808 KB Output is correct
13 Correct 36 ms 18316 KB Output is correct
14 Correct 57 ms 19680 KB Output is correct
15 Correct 77 ms 24504 KB Output is correct
16 Correct 59 ms 16912 KB Output is correct
17 Correct 61 ms 14884 KB Output is correct
18 Correct 17 ms 5772 KB Output is correct
19 Correct 58 ms 19536 KB Output is correct
20 Correct 72 ms 24424 KB Output is correct
21 Correct 54 ms 16960 KB Output is correct
22 Correct 47 ms 14936 KB Output is correct
23 Correct 58 ms 21112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 436 KB Output is correct
9 Correct 2 ms 576 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
11 Correct 9 ms 3540 KB Output is correct
12 Correct 16 ms 5808 KB Output is correct
13 Correct 36 ms 18316 KB Output is correct
14 Correct 57 ms 19680 KB Output is correct
15 Correct 77 ms 24504 KB Output is correct
16 Correct 59 ms 16912 KB Output is correct
17 Correct 61 ms 14884 KB Output is correct
18 Correct 17 ms 5772 KB Output is correct
19 Correct 58 ms 19536 KB Output is correct
20 Correct 72 ms 24424 KB Output is correct
21 Correct 54 ms 16960 KB Output is correct
22 Correct 47 ms 14936 KB Output is correct
23 Correct 58 ms 21112 KB Output is correct
24 Correct 1 ms 316 KB Output is correct
25 Correct 7 ms 3668 KB Output is correct
26 Correct 15 ms 5844 KB Output is correct
27 Correct 34 ms 18296 KB Output is correct
28 Correct 54 ms 19728 KB Output is correct
29 Correct 116 ms 24560 KB Output is correct
30 Correct 70 ms 16984 KB Output is correct
31 Correct 60 ms 14968 KB Output is correct
32 Correct 14 ms 5776 KB Output is correct
33 Correct 53 ms 19732 KB Output is correct
34 Correct 102 ms 24436 KB Output is correct
35 Correct 56 ms 17012 KB Output is correct
36 Correct 65 ms 15032 KB Output is correct
37 Correct 60 ms 21128 KB Output is correct
38 Correct 58 ms 30036 KB Output is correct