답안 #937698

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
937698 2024-03-04T11:34:51 Z 8pete8 Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 11324 KB
#include "catdog.h"
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<cassert>
#include<limits.h>
#include<cmath>
#include<set>
#include<numeric> //gcd(a,b)
#include<algorithm>
#include<bitset> 
#include<stack>
using namespace std;
#define ll long long
#define f first
//#define endl "\n"
#define s second
#define pii pair<int,int>
#define pppiiii pair<pii,pii>
#define ppii pair<int,pii>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
//#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
//#define int long long 
const int mod=1e9+7,mxn=2e5,inf=1e9,minf=-1e18,Mxn=2e6,lg=63;
int root;
void setIO(string name){
	ios_base::sync_with_stdio(0); cin.tie(0);		
	freopen((name+".in").c_str(),"r",stdin);		
	freopen((name+".out").c_str(),"w",stdout);	
}	
vector<int>adj[mxn+10];
int have[mxn+10],dp[mxn+10][2],n;
void caldp(int cur,int p){
	for(auto i:adj[cur]){
		if(i==p)continue;
		caldp(i,cur);
		for(int x=0;x<2;x++){
			dp[cur][x]+=min(dp[i][x],dp[i][x^1]+1);
		}
	}
	//cout<<cur<<" "<<dp[cur][0]<<" "<<dp[cur][1]<<'\n';
}
void init(){
	for(int i=1;i<=n;i++){
		dp[i][0]=dp[i][1]=0;
		if(have[i])dp[i][(have[i]-1)^1]=inf;
	}
}
void initialize(int N,vector<int>A,vector<int>B){
	n=N;
	for(int i=0;i<n-1;i++){
		adj[A[i]].pb(B[i]);
		adj[B[i]].pb(A[i]);
	}
}
int cat(int v){//0 in dp,1 in have
	have[v]=1;
	init();
	caldp(1,-1);
	return min(dp[1][0],dp[1][1]);
}
int dog(int v){
	have[v]=2;
	init();
	caldp(1,-1);
	return min(dp[1][0],dp[1][1]);
}
int neighbor(int v){
	have[v]=0;
	init();
	caldp(1,-1);
	return min(dp[1][0],dp[1][1]);
}
/*

each node can either be cat or dog
dp[i][2]
cost of node i being (cat or dog)
by being cat(or dog) it allowed cat(or dog) to walk pass
if node i has a cat then node i cant be a dog

transition
dp[i][x]->x=1 is cant,2 is dog
dp[i][x]=sum of j in children min(dp[j][x],dp[j][x^1]+1);
so we can use the other one with the cost of one
this solutionn is o(n^2)

can we do better?
hld on dp?
when will it overtake?

*/

Compilation message

catdog.cpp:35:42: warning: overflow in conversion from 'double' to 'int' changes value from '-1.0e+18' to '-2147483648' [-Woverflow]
   35 | const int mod=1e9+7,mxn=2e5,inf=1e9,minf=-1e18,Mxn=2e6,lg=63;
      |                                          ^~~~~
catdog.cpp: In function 'void setIO(std::string)':
catdog.cpp:39:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |  freopen((name+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
catdog.cpp:40:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |  freopen((name+".out").c_str(),"w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 3 ms 7260 KB Output is correct
4 Correct 2 ms 7256 KB Output is correct
5 Correct 2 ms 7256 KB Output is correct
6 Correct 2 ms 7260 KB Output is correct
7 Correct 2 ms 7260 KB Output is correct
8 Correct 2 ms 7260 KB Output is correct
9 Correct 2 ms 7260 KB Output is correct
10 Correct 2 ms 7308 KB Output is correct
11 Correct 2 ms 7260 KB Output is correct
12 Correct 2 ms 7260 KB Output is correct
13 Correct 2 ms 7560 KB Output is correct
14 Correct 3 ms 7260 KB Output is correct
15 Correct 2 ms 7260 KB Output is correct
16 Correct 2 ms 7308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 3 ms 7260 KB Output is correct
4 Correct 2 ms 7256 KB Output is correct
5 Correct 2 ms 7256 KB Output is correct
6 Correct 2 ms 7260 KB Output is correct
7 Correct 2 ms 7260 KB Output is correct
8 Correct 2 ms 7260 KB Output is correct
9 Correct 2 ms 7260 KB Output is correct
10 Correct 2 ms 7308 KB Output is correct
11 Correct 2 ms 7260 KB Output is correct
12 Correct 2 ms 7260 KB Output is correct
13 Correct 2 ms 7560 KB Output is correct
14 Correct 3 ms 7260 KB Output is correct
15 Correct 2 ms 7260 KB Output is correct
16 Correct 2 ms 7308 KB Output is correct
17 Correct 7 ms 7256 KB Output is correct
18 Correct 9 ms 7256 KB Output is correct
19 Correct 5 ms 7260 KB Output is correct
20 Correct 2 ms 7260 KB Output is correct
21 Correct 3 ms 7332 KB Output is correct
22 Correct 4 ms 7264 KB Output is correct
23 Correct 10 ms 7372 KB Output is correct
24 Correct 8 ms 7628 KB Output is correct
25 Correct 4 ms 7260 KB Output is correct
26 Correct 3 ms 7264 KB Output is correct
27 Correct 3 ms 7272 KB Output is correct
28 Correct 3 ms 7272 KB Output is correct
29 Correct 13 ms 7272 KB Output is correct
30 Correct 3 ms 7264 KB Output is correct
31 Correct 3 ms 7260 KB Output is correct
32 Correct 3 ms 7260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 3 ms 7260 KB Output is correct
4 Correct 2 ms 7256 KB Output is correct
5 Correct 2 ms 7256 KB Output is correct
6 Correct 2 ms 7260 KB Output is correct
7 Correct 2 ms 7260 KB Output is correct
8 Correct 2 ms 7260 KB Output is correct
9 Correct 2 ms 7260 KB Output is correct
10 Correct 2 ms 7308 KB Output is correct
11 Correct 2 ms 7260 KB Output is correct
12 Correct 2 ms 7260 KB Output is correct
13 Correct 2 ms 7560 KB Output is correct
14 Correct 3 ms 7260 KB Output is correct
15 Correct 2 ms 7260 KB Output is correct
16 Correct 2 ms 7308 KB Output is correct
17 Correct 7 ms 7256 KB Output is correct
18 Correct 9 ms 7256 KB Output is correct
19 Correct 5 ms 7260 KB Output is correct
20 Correct 2 ms 7260 KB Output is correct
21 Correct 3 ms 7332 KB Output is correct
22 Correct 4 ms 7264 KB Output is correct
23 Correct 10 ms 7372 KB Output is correct
24 Correct 8 ms 7628 KB Output is correct
25 Correct 4 ms 7260 KB Output is correct
26 Correct 3 ms 7264 KB Output is correct
27 Correct 3 ms 7272 KB Output is correct
28 Correct 3 ms 7272 KB Output is correct
29 Correct 13 ms 7272 KB Output is correct
30 Correct 3 ms 7264 KB Output is correct
31 Correct 3 ms 7260 KB Output is correct
32 Correct 3 ms 7260 KB Output is correct
33 Execution timed out 3036 ms 11324 KB Time limit exceeded
34 Halted 0 ms 0 KB -