Submission #537847

# Submission time Handle Problem Language Result Execution time Memory
537847 2022-03-15T16:53:42 Z myrcella Tropical Garden (IOI11_garden) C++17
0 / 100
14 ms 24404 KB
#include "garden.h"
#include "gardenlib.h"

#include<bits/stdc++.h>
using namespace std;
/*
#define MAX_M  1000000
#define MAX_Q  2000

static int N, M, P, Q;
static int R[MAX_M][2];
static int G[MAX_Q];
static int solutions[MAX_Q];
static int answers[MAX_Q];
static int answer_count;


void read_input()
{
  int i;
  scanf("%d %d %d",&N,&M,&P);
  for(i=0; i<M; i++)
   scanf("%d %d",&R[i][0],&R[i][1]);
  scanf("%d",&Q);
  for(i=0; i<Q; i++)
    scanf("%d",&G[i]);
  for(i=0; i<Q; i++)
    scanf("%d",&solutions[i]);
}

void answer(int x)
{
  if(answer_count>=Q) {
    printf("Incorrect.  Too many answers.\n");
    exit(0);
  }
  answers[answer_count] = x;
  answer_count++;
}

*/

#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}

const int maxn = 3e5;

int best[maxn],best2[maxn];
vector <int> edge[maxn][2];
int len[2]={0,0};
bool vis[maxn][2];
int dis[maxn][2][2];
int ans[maxn][2];
pq <pii,vector<pii>,greater<pii> > q;
int p;

void dfs(int u,int rk,int cnt,int id) {
	if (vis[u][rk]) return;
	vis[u][rk]=true;
	int v = best[u];
	if (rk==1 and best2[u]!=-1) v = best2[u];
  	if (u==p and cnt>1 and (id==0 and best[u]==v or id==1 and best2[u]==v)) {
      	len[id]=cnt;
      	return;
    }     
	dfs(v,(best[v]==u),cnt+1,id);
}

void dijkstra(int id) {
	while (!q.empty()) {
		int uu = q.top().se;
		int d = q.top().fi;
		int u = uu%maxn,a=uu/maxn;
		q.pop();
		if (dis[u][a][id]!=d) continue;
		if (a==0) {
			ans[d][id]++;
			for (int v:edge[u][0]) if (dis[v][0][id]>d+1) {
				dis[v][0][id]=d+1;
				q.push(MP(d+1,v));
			}
			for (int v:edge[u][1]) if (dis[v][1][id]>d+1) {
				dis[v][1][id]=d+1;
				q.push(MP(d+1,v+maxn));
			}
		}
		else {
			if (best[best[u]]==u&&dis[best[u]][0][id]>d+1) {
				dis[best[u]][0][id]=d+1;
				q.push(MP(d+1,best[u]));
			}
			else if (best2[best[u]]==u&&dis[best[u]][1][id]>d+1) {
				dis[best[u]][1][id]=d+1;
				q.push(MP(d+1,best[u]));
			}
		}
	}
}

void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
	p = P;
	memset(best,-1,sizeof(best));
	memset(best2,-1,sizeof(best2));
	memset(dis,inf,sizeof(dis));
	memset(ans,0,sizeof(ans));
	rep(i,0,M) {
		if (best[R[i][0]]==-1) best[R[i][0]] = R[i][1],edge[R[i][1]][0].pb(R[i][0]);
		else if (best2[R[i][0]]==-1) best2[R[i][0]] = R[i][1],edge[R[i][1]][1].pb(R[i][0]);
		if (best[R[i][1]]==-1) best[R[i][1]] = R[i][0],edge[R[i][0]][0].pb(R[i][1]);
		else if (best2[R[i][1]]==-1) best2[R[i][1]] = R[i][0],edge[R[i][0]][1].pb(R[i][1]);
	}
	
	memset(vis,0,sizeof(vis));
	dfs(best[p],best[best[p]]==p,1,0);
	dis[p][0][0] = 0;
	q.push(MP(0,p));
	dijkstra(0);
	
	if(best2[p]!=-1) {
	
		memset(vis,0,sizeof(vis));
		dfs(best2[p],best[best2[p]]==p,1,1);
		dis[p][1][1] = 0;
		q.push(MP(0,maxn+p));
		dijkstra(1);
	}
	
	rep(i,0,Q) {
		int res = 0;
		rep(j,0,2) {
			if (len[j]==0 and G[i]<maxn) res+=ans[G[i]][j];
			else if (len[j]>0) for (int k=G[i]%len[j];k<maxn&&k<G[i];k+=len[j]) res+=ans[k][j];
		}
		answer(res);
	}
}
/*
int main()
{
  int correct, i;

  read_input();
  answer_count = 0;
  count_routes(N,M,P,R,Q,G);

  if(answer_count!=Q) {
    printf("Incorrect.  Too few answers.\n");
    exit(0);
  }

  correct = 1;
  for(i=0; i<Q; i++)
    if(answers[i]!=solutions[i])
      correct = 0;
  if(correct)
    printf("Correct.\n");
  else {
    printf("Incorrect.\n");
    printf("Expected: ");
    for(i=0; i<Q; i++)
      printf("%d ",solutions[i]);
    printf("\nReturned: ");
    for(i=0; i<Q; i++)
      printf("%d ",answers[i]);
  }
  return 0;
}
*/

Compilation message

garden.cpp: In function 'void dfs(int, int, int, int)':
garden.cpp:78:34: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   78 |    if (u==p and cnt>1 and (id==0 and best[u]==v or id==1 and best2[u]==v)) {
      |                            ~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 24404 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 24404 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 24404 KB Output isn't correct
2 Halted 0 ms 0 KB -