QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#104856#4229. GCD HarmonyYanagi_Origami#WA 3ms3432kbC++201.5kb2023-05-12 07:37:012023-05-12 07:37:01

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-12 07:37:01]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3432kb
  • [2023-05-12 07:37:01]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define forn(i,a,b) for(int i = a; i < (b); i++)
#define fore(i,a,b) for(int i = a; i <= (b); i++)
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using ll = long long;
using ld = long double;

static constexpr ll kInf = 1'000'000'000'000'000'000;
static constexpr int kMaxA = 100;

void dfs(const vector<vector<int>> &adj, const vector<int>& a,
         vector<vector<ll>>& dp, const int u, const int p)
{
    for (int v : adj[u])
    {
        if (v == p) continue;
        dfs(adj, a, dp, v, u);
    }

    for (int j = 1; j <= kMaxA; j++)
    {
        ll sum = j;
        if (j == a[u]) sum = 0;  // no change needed
        for (int v : adj[u])
        {
            if (v == p) continue;

            ll mn = kInf;
            for (int k = 1; k <= kMaxA; k++)
            {
                if (gcd(k, j) == 1) continue;
                mn = min(mn, dp[v][k]);
            }
            sum += mn;
        }
        dp[u][j] = sum;
    }
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);

    int n; cin>>n;
    vector<int> a(n);
    forn(i,0,n) cin>>a[i];

    vector<vector<int>> adj(n);
    forn(i,0,n-1)
    {
        int u,v; cin>>u>>v; u--; v--;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    vector<vector<ll>> dp(n, vector<ll>(kMaxA + 1, 0));
    dfs(adj, a, dp, 0, -1);
    
    ll ans = kInf;
    fore(i, 1, kMaxA) ans = min(ans, dp[0][i]);
    cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 3428kb

input:

6
5
6
3
4
9
12
1 2
1 3
1 4
1 6
3 5

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3388kb

input:

3
1
2
3
3 1
2 3

output:

4

result:

ok single line: '4'

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3432kb

input:

13
2
5
5
5
5
5
5
3
3
3
3
3
3
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13

output:

-6446744073709551615

result:

wrong answer 1st lines differ - expected: '15', found: '-6446744073709551615'