Submission #72623

# Submission time Handle Problem Language Result Execution time Memory
72623 2018-08-26T12:12:22 Z ekrem Cats or Dogs (JOI18_catdog) C++
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
#include "catdog.h"
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define inf 1000000007
#define N 1000005
using namespace std;

typedef vector < int > vi;

int a[N], dp[2][N];
vi gg[N], g[N];

int f(int node, int dur){
	int &r = dp[dur][node];
	if(r != -1)
		return r;
	r = 0;
	if(a[node] and a[node] - 1 != dur)
		return r = inf;
	for(int i = 0; i < g[node].size(); i++){
		int coc = g[node][i];
		r += min(f(coc, dur), f(coc, !dur) + 1);
	}
	// if(a[1] == 2)
	// 	cout << node << " " << dur << " -> " << r << endl;
	return r;
}

void hazirla(int node, int par){
	for(int i = 0; i < gg[node].size(); i++)
		if(gg[node][i] != par){
			g[node].pb(gg[node][i]);
			hazirla(gg[node][i], node);
		}
}

void initialize(int n, vi a, vi b){
	for(int i = 0; i < n - 1; i++){
		gg[a[i]].pb(b[i]);
		gg[b[i]].pb(a[i]);
	}
	hazirla(1, -1);
}

int cat(int v){
	memset(dp, -1, sizeof dp);
	a[v] = 1;
	return min(f(1, 0), f(1, 1));
}

int dog(int v){
	memset(dp, -1, sizeof dp);
	a[v] = 2;
	return min(f(1, 0), f(1, 1));
}

int neighbor(int v){
	memset(dp, -1, sizeof dp);
	a[v] = 0;
	return min(f(1, 0), f(1, 1));
}


int main(){
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	int n, q;
	scanf("%d",&n);
	vi a(n - 1), b(n - 1);
	for(int i = 0; i < n - 1; i++)
		scanf("%d %d",&a[i] ,&b[i]);
	scanf("%d",&q);
	vi t(q), v(q);
	for(int i = 0; i < q; i++)
		scanf("%d %d",&t[i] ,&v[i]);

	initialize(n, a, b);
	vi ans(q);
	for(int i = 0; i < q; i++){
		if(t[i] == 1)
			ans[i] = cat(v[i]);
		else if(t[i] == 2)
			ans[i] = dog(v[i]);
		else
			ans[i] = neighbor(v[i]);
	}
	for(int i = 0; i < q; i++)
		printf("%d\n",ans[i]);
	return 0;
}

Compilation message

catdog.cpp: In function 'int f(int, int)':
catdog.cpp:23:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < g[node].size(); i++){
                 ~~^~~~~~~~~~~~~~~~
catdog.cpp: In function 'void hazirla(int, int)':
catdog.cpp:33:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < gg[node].size(); i++)
                 ~~^~~~~~~~~~~~~~~~~
catdog.cpp: In function 'int main()':
catdog.cpp:68:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  freopen("in.txt", "r", stdin);
  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
catdog.cpp:69:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  freopen("out.txt", "w", stdout);
  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
catdog.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
catdog.cpp:74:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&a[i] ,&b[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
catdog.cpp:75:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&q);
  ~~~~~^~~~~~~~~
catdog.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&t[i] ,&v[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
/tmp/cci1YKTW.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccbzDqpy.o:catdog.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status