제출 #1119006

#제출 시각아이디문제언어결과실행 시간메모리
1119006RequiemCats or Dogs (JOI18_catdog)C++17
컴파일 에러
0 ms0 KiB
//#include "catdog.h"
#include<bits/stdc++.h>
//#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define inf 1e9
#define fi first
#define se second
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define endl '\n'
#define pi 3.14159265359
#define TASKNAME "catdogs"
using namespace std;
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
/**
Cat / Dogs:

Cho 1 cây n đỉnh với n - 1 cạnh.

Một số đỉnh có màu đỏ / xanh.

Tìm tập cạnh tối thiểu sao cho:
==> trên đường đi giữa 1 cặp đỉnh xanh / đỏ bất kì đều tồn tại 1 cạnh trong tập đã chọn

subtask 2: N <= 1000, Q <= 1000.
O(N * Q).
Ta dựng cây lên.
Với 1 subtree của thằng u, ta nhận coi là nó là thằng blue / red.

Nếu số thằng blue > red thì mình cho blue lên, và chặn hết đống red.
**/
const int MAXN = 3e5 + 9;
int n;
ii edge[MAXN];
vector<int> g[MAXN];
bool catNode[MAXN], dogNode[MAXN];

namespace subtask3{
    bool check(){
        return true;
    }

    int par[MAXN], dp[MAXN], val[MAXN];

    void dfs(int u, int p){
        par[u] = p;

        for(auto id: g[u]){
            int v = edge[id].fi + edge[id].se - u;
            if (v == p) continue;
            dfs(v, u);
        }
    }

    void recalc(int u){
        int numDog = 0;
        int numCat = 0;

        for(auto id: g[u]){
            int v = edge[id].fi + edge[id].se - u;
            if (v == par[u]) continue;

            val[u] += val[v];

            if (dp[v] > 0) numDog++;
            else if (dp[v] < 0) numCat++;
        }

        if (dogNode[u]) {
            val[u] += numCat;
            dp[u] = 1;
        }
        else if (catNode[u]){
            val[u] += numDog;
            dp[u] = -1;
        }
        else {
            val[u] += min(numCat, numDog);
            if (numCat < numDog) dp[u] = 1;
            else if (numCat > numDog) dp[u] = -1;
        }
    }

    int update(int u){
        while(true){
            recalc(u);
            if (u == 1) break;
            u = par[u];
        }
        return val[1];
    }

    void init(){
        dfs(1, -1);
    }
}
void initialize(int N, vector<int> A, vector<int> B){
    n = N;
    memset(catNode, false, sizeof(catNode));
    memset(dogNode, false, sizeof(dogNode));
    FOR(i, 0, n - 2){
        edge[i] = {A[i], B[i]};
        g[A[i]].pb(i);
        g[B[i]].pb(i);
    }
    subtask3::dfs(1, -1);
}

int cat(int u){
    catNode[u] = true;
    return subtask3::update(u);

}

int dog(int u){
    dogNode[u] = true;
    return subtask3::update(u);
}

int neighbor(int u){
    catNode[u] = dogNode[u] = false;
    return subtask3::update(u);
}

main()
{
    fast;
    if (fopen(TASKNAME".inp","r")){
        freopen(TASKNAME".inp","r",stdin);
        freopen(TASKNAME".out","w",stdout);
    }
    int n, q;
    vector<int> a, b;
    cin >> n >> q;
    FOR(i, 1, n - 1){
        int u, v;
        cin >> u >> v;
        a.pb(u);
        b.pb(v);
    }
    initialize(n, a, b);

    FOR(i, 1, q){
        int u;
        string type;
        cin >> type >> u;
        if (type == "dog") {
            cout << dog(u) << endl;
        }
        else if (type == "cat"){
            cout << cat(u) << endl;
        }
        else{
            cout << neighbor(u) << endl;
        }
    }

}
/**
Warning:
Đọc sai đề???
Cận lmao
Code imple thiếu case nào không.
Limit.
**/

컴파일 시 표준 에러 (stderr) 메시지

catdog.cpp:132:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  132 | main()
      | ^~~~
catdog.cpp: In function 'int main()':
catdog.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |         freopen(TASKNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
catdog.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  137 |         freopen(TASKNAME".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccT6rVps.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cctoAaSu.o:catdog.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status