답안 #162217

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
162217 2019-11-07T06:34:40 Z HellAngel Mousetrap (CEOI17_mousetrap) C++14
45 / 100
1235 ms 107728 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;
const int maxn = 1e6 + 7;
int FastIn()
{
    char c;
    while(true)
    {
        c = getchar();
        if(c >= '0' && c <= '9') break;
    }
    int ans = c- '0';
    while(true)
    {
        c = getchar();
        if(c > '9' || c < '0') return ans;
        ans = ans * 10 + c - '0';
    }
}
int n, m, t, cnt, sum, dp[maxn];

multiset<int> st;

bool found;
vector<int> vt[maxn], path, gau[maxn];

void DFS(int u, int p)
{
    if(u == t)
    {
        path.push_back(u);
        found = true;
        return;
    }
    for(auto v: vt[u])
    {
        if(v == p) continue;
        DFS(v, u);
        if(found)
        {
            path.push_back(u);
            break;
        }
    }
}

void Solve(int u, int p)
{
    vector<int> cac = {};
    int sum = -1;
    for(auto v: vt[u])
    {
        if(v == p) continue;
        Solve(v, u);
        sum++;
        cac.push_back(dp[v]);
    }
    sort(cac.begin(), cac.end());
    if(cac.size())
    {
        cac.pop_back();
        if(cac.size() == 0) dp[u] = 1;
        else dp[u] = sum + cac.back() + 1;
    }
    else dp[u] = 0;
}

bool Check(int x)
{
    st.clear();
    for(int i = 0; i + 1 < path.size(); i++)
    {
        gau[i + 1] = {};
        int u = path[i];
        for(auto v: vt[u])
        {
            if(v == path[i + 1]) continue;
            if(i > 0 && v == path[i - 1]) continue;
            gau[i + 1].push_back(dp[v]);
            if(dp[v] >= x) st.insert(i + 1);
        }
    }
    int cnt = path.size() - 1;
    for(int i = 1; i <= cnt; i++) sort(gau[i].begin(), gau[i].end());
    for(int i = 1; i <= cnt; i++)
    {
        if(st.empty()) return true;
        st.erase(st.begin());
        if(st.empty()) return true;
        if(*st.begin() <= i) return false;
    }
    return true;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if(fopen("test.inp", "r")) freopen("test.inp", "r", stdin), freopen("test.out", "w", stdout);
    n = FastIn();
    t = FastIn();
    m = FastIn();
    for(int i = 1; i < n; i++)
    {
        int u, v;
        u = FastIn();
        v = FastIn();
        vt[u].push_back(v);
        vt[v].push_back(u);
    }
    DFS(m, m);
    reverse(path.begin(), path.end());
    for(int i = 0; i + 1 < path.size(); i++)
    {
        int u = path[i];
        for(auto v: vt[u])
        {
            if(v == path[i + 1]) continue;
            if(i > 0 && v == path[i - 1]) continue;
            Solve(v, u);
            sum++;
        }
    }
    int l = 1;
    int r = 1e9;
    while(l <= r)
    {
        int mid = l + r >> 1;
        if(Check(mid)) r = mid - 1;
        else l = mid + 1;
    }
    cout << sum + r;
}

Compilation message

mousetrap.cpp: In function 'bool Check(long long int)':
mousetrap.cpp:73:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i + 1 < path.size(); i++)
                    ~~~~~~^~~~~~~~~~~~~
mousetrap.cpp: In function 'int32_t main()':
mousetrap.cpp:115:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i + 1 < path.size(); i++)
                    ~~~~~~^~~~~~~~~~~~~
mousetrap.cpp:130:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         int mid = l + r >> 1;
                   ~~^~~
mousetrap.cpp:101:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     if(fopen("test.inp", "r")) freopen("test.inp", "r", stdin), freopen("test.out", "w", stdout);
                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:101:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 47352 KB Output is correct
2 Correct 47 ms 47352 KB Output is correct
3 Correct 49 ms 47356 KB Output is correct
4 Correct 49 ms 47352 KB Output is correct
5 Correct 47 ms 47272 KB Output is correct
6 Correct 47 ms 47256 KB Output is correct
7 Correct 49 ms 47364 KB Output is correct
8 Correct 48 ms 47352 KB Output is correct
9 Correct 49 ms 47352 KB Output is correct
10 Correct 48 ms 47352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 495 ms 107728 KB Output is correct
2 Correct 452 ms 101704 KB Output is correct
3 Correct 1160 ms 105976 KB Output is correct
4 Correct 569 ms 76408 KB Output is correct
5 Correct 1235 ms 105848 KB Output is correct
6 Correct 1199 ms 105720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 47352 KB Output is correct
2 Correct 47 ms 47352 KB Output is correct
3 Correct 49 ms 47356 KB Output is correct
4 Correct 49 ms 47352 KB Output is correct
5 Correct 47 ms 47272 KB Output is correct
6 Correct 47 ms 47256 KB Output is correct
7 Correct 49 ms 47364 KB Output is correct
8 Correct 48 ms 47352 KB Output is correct
9 Correct 49 ms 47352 KB Output is correct
10 Correct 48 ms 47352 KB Output is correct
11 Correct 47 ms 47368 KB Output is correct
12 Correct 50 ms 47352 KB Output is correct
13 Correct 49 ms 47352 KB Output is correct
14 Correct 49 ms 47480 KB Output is correct
15 Correct 49 ms 47484 KB Output is correct
16 Incorrect 49 ms 47352 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 47352 KB Output is correct
2 Correct 47 ms 47352 KB Output is correct
3 Correct 49 ms 47356 KB Output is correct
4 Correct 49 ms 47352 KB Output is correct
5 Correct 47 ms 47272 KB Output is correct
6 Correct 47 ms 47256 KB Output is correct
7 Correct 49 ms 47364 KB Output is correct
8 Correct 48 ms 47352 KB Output is correct
9 Correct 49 ms 47352 KB Output is correct
10 Correct 48 ms 47352 KB Output is correct
11 Correct 495 ms 107728 KB Output is correct
12 Correct 452 ms 101704 KB Output is correct
13 Correct 1160 ms 105976 KB Output is correct
14 Correct 569 ms 76408 KB Output is correct
15 Correct 1235 ms 105848 KB Output is correct
16 Correct 1199 ms 105720 KB Output is correct
17 Correct 47 ms 47368 KB Output is correct
18 Correct 50 ms 47352 KB Output is correct
19 Correct 49 ms 47352 KB Output is correct
20 Correct 49 ms 47480 KB Output is correct
21 Correct 49 ms 47484 KB Output is correct
22 Incorrect 49 ms 47352 KB Output isn't correct
23 Halted 0 ms 0 KB -