답안 #531788

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
531788 2022-03-01T13:49:08 Z Killer2501 Šarenlist (COCI22_sarenlist) C++14
110 / 110
10 ms 332 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<ll, int>
#define fi first
#define se second
using namespace std;
const int N = 65;
const int M = 250;
const int mod = 1e9+7;
const ll base = 75;
const ll inf = 1e16;
int n, lab[N], t, c[N], h[N], P[N][8], m, a[N], b[N], ans, d[N], k;
ll st[N], sum[N][8], tong;
vector<int> adj[N];
vector<int> vi;
string s;
bool vis[N];
struct edge
{
    int x, y, z;
}e[N];
void add(int& x, int y)
{
    x += y;
    if(x >= mod)x -= mod;
}
void dfs(int u, int p = 0)
{
    for(int v: adj[u])
    {
        if(v == p)continue;
        h[v] = h[u]+1;
        P[v][0] = u;
        sum[v][0] = (1ll<<v);
        for(int j = 1; j < 8; j ++)
        {
            P[v][j] = P[P[v][j-1]][j-1];
            sum[v][j] = (sum[v][j-1] | sum[P[v][j-1]][j-1]);
        }
        dfs(v, u);
    }
}
ll lca(int u, int v)
{
    ll res = 0;
    if(h[u] < h[v])swap(u, v);
    int log = log2(h[u]);
    for(int i = log; i >= 0; i --)
    {
        if(h[u] >= h[v]+(1<<i))
        {
            res |= sum[u][i];
            u = P[u][i];
        }
    }
    if(u == v)return res;
    for(int i = log; i >= 0; i --)
    {
        if(P[u][i] && P[u][i] != P[v][i])
        {
            res |= sum[u][i];
            res |= sum[v][i];
            u = P[u][i];
            v = P[v][i];
        }
    }
    res |= sum[u][0];
    res |= sum[v][0];
    return res;
}
int findp(int u)
{
    return lab[u] < 0 ? u : lab[u] = findp(lab[u]);
}
void hop(int u, int v)
{
    u = findp(u);
    v = findp(v);
    if(u == v)return;
    if(lab[u] > lab[v])swap(u, v);
    lab[u] += lab[v];
    lab[v] = u;
}
void sol()
{
    cin >> n >> m >> k;
    for(int i = 1; i < n; i ++)
    {
        int x, y;
        cin >> x >> y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    d[0] = 1;
    for(int i = 1; i < n; i ++)d[i] = 1ll * d[i-1] * k % mod;
    dfs(1);
    for(int i = 0; i < m; i ++)
    {
        cin >> e[i].x >> e[i].y;
        st[i] = lca(e[i].x, e[i].y);
       //cout << st[i] <<" ";
    }
    //cout << '\n';
    for(int mask = 0; mask < (1<<m); mask ++)
    {
        tong = 0;
        t = 0;
        fill_n(lab, m, -1);
        vi.clear();
        for(int i = 0; i < m; i ++)
        {
            if(mask >> i & 1)
            {
                //cout << i <<" ";
                for(int j: vi)if(st[i]&st[j])hop(i, j);
                vi.pb(i);
                tong |= st[i];
            }
        }
        for(int i: vi)if(lab[i] < 0)++t;
        //cout <<t << '\n';
        t = t+n-1-(__builtin_popcountll(tong));
        if((__builtin_popcount(mask))&1)add(ans, mod-d[t]);
        else add(ans, d[t]);
    }
    cout << ans;
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    #define task "test"
    if(fopen(task".inp", "r"))
    {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int test = 1;
    //cin >> test;
    while(test -- > 0)sol();
    return 0;
}
/*
1234
21
*/

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 3 ms 204 KB Output is correct
6 Correct 3 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 312 KB Output is correct
24 Correct 3 ms 204 KB Output is correct
25 Correct 3 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 5 ms 316 KB Output is correct
28 Correct 1 ms 312 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 6 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 1 ms 312 KB Output is correct
33 Correct 1 ms 324 KB Output is correct
34 Correct 1 ms 312 KB Output is correct
35 Correct 2 ms 332 KB Output is correct
36 Correct 10 ms 316 KB Output is correct
37 Correct 5 ms 204 KB Output is correct