UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213529#573. t2ThySecret0431ms24276kbC++112.7kb2024-11-12 20:08:532024-11-12 23:22:16

answer

#include <bits/stdc++.h>

using namespace std;

// #define int long long
#define x first
#define y second
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)

inline void debug() { cerr << '\n'; }
template<typename Type, typename... Other>
inline void debug(const Type& x, const Other&... y) { cerr << x << ' '; debug(y...); }
#define DEBUG(a...) cerr << "[" << #a << "] = ", debug(a);

typedef long long LL;
typedef pair<int, int> PII;

const int N = 200010;
const int INF = 0x3f3f3f3f;

template<typename Type>
inline void read(Type &res)
{
    res = 0;
    int ch = getchar(), flag = 0;
    while (!isdigit(ch)) flag |= ch == '-', ch = getchar();
    while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
    res = flag ? -res : res;
}
template<typename Type, typename... Other>
inline void read(Type &res, Other&... y) { read(res), read(y...); }

int n, q;
int h[N], e[N << 1], ne[N << 1], w[N << 1], idx;
int sz[N], pre[N], stk[N], top;
vector<PII> g[N];
int ans[N], vis[N];

inline void add(int a, int b, int c) { e[++ idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx; }

int rt(int x) { return pre[x] == x ? x : pre[x] = rt(pre[x]); }

inline void merge(int a, int b)
{
    int pa = rt(a), pb = rt(b);
    if (pa == pb) return;
    sz[pb] += sz[pa], pre[pa] = pb;
}

inline void divide(int x, int a, int b)
{
    g[1].emplace_back(a, b), g[x].emplace_back(a, b);
    for (int i = 2; i <= x / i; i ++)
        if (x % i == 0)
        {
            g[i].emplace_back(a, b);
            if (i != x / i) g[x / i].emplace_back(a, b);
        }
}

signed main()
{
    iota(pre, pre + N, 0);
    for (int i = 1; i < N; i ++) sz[i] = 1;
    // memset(h, -1, sizeof h), idx = -1;
    read(n, q);
    for (int i = 1; i < n; i ++)
    {
        int a, b, c; read(a, b, c);
        add(a, b, c), add(b, a, c);
        divide(c, a, b);
    }

    for (int cur = 1; cur <= n; cur ++)
    {
        for (PII ver : g[cur])
        {
            int u = ver.x, v = ver.y;
            // stk[++ top] = u, stk[++ top] = v;
            if (vis[u] != cur) vis[u] = cur, stk[++ top] = u;
            if (vis[v] != cur) vis[v] = cur, stk[++ top] = v;
            merge(u, v);
        }
        for (int ver = 1; ver <= top; ver ++)
        {
            if (rt(stk[ver]) == stk[ver])
            {
                ans[cur] += (sz[stk[ver]] * (sz[stk[ver]] - 1)) / 2;
                sz[stk[ver]] = 1;
            }
        }
        while (top) pre[stk[top]] = stk[top], sz[stk[top]] = 1, top --;
    }

    while (q --)
    {
        int x; read(x);
        cout << ans[x] << '\n';
    }

    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 6ms
memory: 7492kb

input:

50 50
48 29 49788
47 48 31142
35 48 28665
10 35 23889
39 35 6411
50 39 66666
43 35 27629
46 10 49173...

output:

2
0
0
0
0
2
0
0
0
0
0
0
1
10
0
0
1
0
0
2
0
0
0
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
2
1
2
0
0
0
0

result:

wrong answer 2nd words differ - expected: '1', found: '0'

Subtask #2:

score: 0
Wrong Answer

Test #11:

score: 0
Wrong Answer
time: 83ms
memory: 15460kb

input:

100000 100000
73595 40695 76
13615 40695 96
65545 13615 84
19391 13615 76
2353 73595 27
26730 40695 ...

output:

12815
1065
2028
53298
627586
19060
4345
1010
16097
1032
1044
1054
3191
16097
1055
627586
19060
978
9...

result:

wrong answer 20th words differ - expected: '4999950000', found: '704982704'

Subtask #3:

score: 0
Wrong Answer

Test #21:

score: 0
Wrong Answer
time: 342ms
memory: 24276kb

input:

100000 100000
73595 40695 12816
13615 73595 81821
65545 40695 75866
19391 65545 1165
2353 73595 3737...

output:

8
2270
96901
1
2085
1584
4228
9112
6
0
2
0
11
1035
2833
0
704982704
2
2
1294
6056
2221
4398
1587
497...

result:

wrong answer 17th words differ - expected: '4999950000', found: '704982704'