제출 #201152

#제출 시각아이디문제언어결과실행 시간메모리
201152SamAndMag (COCI16_mag)C++17
84 / 120
903 ms262148 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 1000006;
int gcd(int x, int y)
{
    while (y)
    {
        x %= y;
        swap(x, y);
    }
    return x;
}
struct ban
{
    int x, y;
    ban()
    {
        x = 0;
        y = 1;
    }
    ban(int x, int y)
    {
        int g = gcd(x, y);
        this->x = x / g;
        this->y = y / g;
    }
};
bool operator<(const ban& a, const ban& b)
{
    return (a.x * 1LL * b.y) < (b.x * 1LL * a.y);
}

int n;
vector<int> a[N];
int u[N];

int q[N];

void dfs(int x, int p)
{
    if (u[x] == 1)
        q[x] = 1;
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i];
        if (h == p)
            continue;
        dfs(h, x);
        if (u[x] == 1)
            q[x] = max(q[x], q[h] + 1);
    }
}

ban ans;

void dfs1(int x, int p, int qp)
{
    multiset<int> s;
    s.insert(qp);
    s.insert(0);
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i];
        if (h == p)
            continue;
        s.insert(q[h]);
    }
    ban t = ban(u[x], *(--s.end()) + 1 + *(--(--s.end())));
    if (t < ans)
        ans = t;
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i];
        if (h == p)
            continue;
        s.erase(s.find(q[h]));
        if (u[x] == 1)
            dfs1(h, x, *(--s.end()) + 1);
        else
            dfs1(h, x, 0);
        s.insert(q[h]);
    }
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n - 1; ++i)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        a[x].push_back(y);
        a[y].push_back(x);
    }
    for (int i = 1; i <= n; ++i)
        scanf("%d", &u[i]);
    dfs(1, 1);
    ans = ban(u[1], 1);
    dfs1(1, 1, 0);
    printf("%d/%d\n", ans.x, ans.y);
    return 0;
}

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

mag.cpp: In function 'void dfs(int, int)':
mag.cpp:43:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
mag.cpp: In function 'void dfs1(int, int, int)':
mag.cpp:61:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
mag.cpp:71:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
mag.cpp: In function 'int main()':
mag.cpp:87:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
mag.cpp:91:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x, &y);
         ~~~~~^~~~~~~~~~~~~~~~
mag.cpp:96:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &u[i]);
         ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...