답안 #660931

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
660931 2022-11-23T14:39:58 Z Trisanu_Das Mag (COCI16_mag) C++14
120 / 120
495 ms 158684 KB
#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)
{
    int max1 = qp;
    int max2 = 0;
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i];
        if (h == p)
            continue;
        if (q[h] >= max1)
        {
            max2 = max1;
            max1 = q[h];
        }
        else if (q[h] >= max2)
            max2 = q[h];
    }
    ban t = ban(u[x], max1 + 1 + max2);
    if (t < ans)
        ans = t;
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i];
        if (h == p)
            continue;
        if (u[x] == 1)
        {
            if (q[h] == max1)
                dfs1(h, x, max2 + 1);
            else
                dfs1(h, x, max1 + 1);
        }
        else
            dfs1(h, x, 0);
    }
}
 
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;
}

Compilation message

mag.cpp: In function 'void dfs(int, int)':
mag.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     for (int i = 0; i < a[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
mag.cpp: In function 'void dfs1(int, int, int)':
mag.cpp:60:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for (int i = 0; i < a[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
mag.cpp:76:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |     for (int i = 0; i < a[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
mag.cpp: In function 'int main()':
mag.cpp:95:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
mag.cpp:99:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |         scanf("%d%d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~
mag.cpp:104:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |         scanf("%d", &u[i]);
      |         ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 13 ms 23728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23800 KB Output is correct
2 Correct 13 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 354 ms 86380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 460 ms 155464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 481 ms 155124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 396 ms 62612 KB Output is correct
2 Correct 308 ms 63696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 390 ms 63764 KB Output is correct
2 Correct 67 ms 29316 KB Output is correct
3 Correct 495 ms 158684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 27784 KB Output is correct
2 Correct 403 ms 78580 KB Output is correct
3 Correct 301 ms 51404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 353 ms 60676 KB Output is correct
2 Correct 407 ms 79156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 420 ms 63312 KB Output is correct
2 Correct 312 ms 51608 KB Output is correct