답안 #512502

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
512502 2022-01-16T12:18:37 Z Killer2501 Election Campaign (JOI15_election_campaign) C++14
100 / 100
253 ms 37192 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<int, int>
#define fi first
#define se second
using namespace std;
const int N = 2e5+5;
const int M = (1<<17);
const ll inf = 1e15;
const int mod = 1e9+7;
int n, t, k, a[N], c[N], d[N], m, P[N][19], h[N], in[N], out[N];
ll ans, tong, b[N], dp[N], fe[N];
string s;
struct node
{
    int val, x, y;
    node(){}
    node(int _val, int _x, int _y)
    {
        val = _val;
        x = _x;
        y = _y;
    }
    bool operator < (const node& u) const
    {
        return x > u.x;
    }
};
vector<node> g[N];
vector<int>  adj[N];
void add(int id, ll x)
{
    for(; id <= n; id += id & -id)fe[id] += x;
}
void add(int l, int r, ll x)
{
    add(l, x);
    add(r+1, -x);
}
ll get(int id)
{
    ll total = 0;
    for(; id; id -= id & -id)total += fe[id];
    return total;
}
void dfs(int u, int p = 0)
{
    in[u] = ++k;
    for(int v: adj[u])
    {
        if(v == p)continue;
        h[v] = h[u]+1;
        P[v][0] = u;
        for(int i = 1; i <= 18; i ++)P[v][i] = P[P[v][i-1]][i-1];
        dfs(v, u);
    }
    out[u] = k;
}
int lca(int u, int v)
{
    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))u = P[u][i];
    if(u == v)return u;
    for(int i = log; i >= 0; i --)
    {
        if(P[u][i] && P[u][i] != P[v][i])
        {
            u = P[u][i];
            v = P[v][i];
        }
    }
    return P[u][0];
}
void cal(int u, int p = 0)
{
    for(int v: adj[u])
    {
        if(v == p)continue;
        cal(v, u);
        add(in[u], out[u], dp[v]);
        add(in[v], out[v], -dp[v]);
    }

    dp[u] = get(in[u]) - get(in[p]);
    for(node t: g[u])
    {
        dp[u] = max(dp[u], get(in[t.x]) + get(in[t.y]) - get(in[u]) - get(in[p]) + t.val);
    }
}
void sol()
{
    cin >> n;
    for(int i = 1; i < n; i ++)
    {
        int x, y;
        cin >> x >> y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    dfs(1);
    cin >> m;
    while(m -- > 0)
    {
        int x, y, z;
        cin >> x >> y >> k;
        z = lca(x, y);
        g[z].pb(node(k, x, y));
    }

    cal(1);
    cout << dp[1];
}
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 ntest = 1;
    //cin >> ntest;
    while(ntest -- > 0)
    sol();
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:126:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:127:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9680 KB Output is correct
2 Correct 5 ms 9720 KB Output is correct
3 Correct 6 ms 9712 KB Output is correct
4 Correct 6 ms 9808 KB Output is correct
5 Correct 93 ms 24316 KB Output is correct
6 Correct 52 ms 33480 KB Output is correct
7 Correct 89 ms 30272 KB Output is correct
8 Correct 77 ms 24724 KB Output is correct
9 Correct 97 ms 28712 KB Output is correct
10 Correct 71 ms 24672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9680 KB Output is correct
2 Correct 5 ms 9680 KB Output is correct
3 Correct 5 ms 9936 KB Output is correct
4 Correct 114 ms 36916 KB Output is correct
5 Correct 130 ms 36900 KB Output is correct
6 Correct 98 ms 36828 KB Output is correct
7 Correct 124 ms 36832 KB Output is correct
8 Correct 116 ms 36972 KB Output is correct
9 Correct 109 ms 36812 KB Output is correct
10 Correct 119 ms 36844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9680 KB Output is correct
2 Correct 5 ms 9680 KB Output is correct
3 Correct 5 ms 9936 KB Output is correct
4 Correct 114 ms 36916 KB Output is correct
5 Correct 130 ms 36900 KB Output is correct
6 Correct 98 ms 36828 KB Output is correct
7 Correct 124 ms 36832 KB Output is correct
8 Correct 116 ms 36972 KB Output is correct
9 Correct 109 ms 36812 KB Output is correct
10 Correct 119 ms 36844 KB Output is correct
11 Correct 14 ms 10744 KB Output is correct
12 Correct 127 ms 37088 KB Output is correct
13 Correct 121 ms 37104 KB Output is correct
14 Correct 119 ms 37180 KB Output is correct
15 Correct 118 ms 37168 KB Output is correct
16 Correct 116 ms 37092 KB Output is correct
17 Correct 124 ms 37064 KB Output is correct
18 Correct 114 ms 37188 KB Output is correct
19 Correct 122 ms 37084 KB Output is correct
20 Correct 129 ms 37164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 27028 KB Output is correct
2 Correct 108 ms 36848 KB Output is correct
3 Correct 207 ms 33236 KB Output is correct
4 Correct 111 ms 27576 KB Output is correct
5 Correct 178 ms 32584 KB Output is correct
6 Correct 109 ms 27640 KB Output is correct
7 Correct 187 ms 32296 KB Output is correct
8 Correct 172 ms 27468 KB Output is correct
9 Correct 125 ms 36800 KB Output is correct
10 Correct 210 ms 31120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9680 KB Output is correct
2 Correct 5 ms 9720 KB Output is correct
3 Correct 6 ms 9712 KB Output is correct
4 Correct 6 ms 9808 KB Output is correct
5 Correct 93 ms 24316 KB Output is correct
6 Correct 52 ms 33480 KB Output is correct
7 Correct 89 ms 30272 KB Output is correct
8 Correct 77 ms 24724 KB Output is correct
9 Correct 97 ms 28712 KB Output is correct
10 Correct 71 ms 24672 KB Output is correct
11 Correct 6 ms 9856 KB Output is correct
12 Correct 6 ms 9936 KB Output is correct
13 Correct 8 ms 9988 KB Output is correct
14 Correct 7 ms 9860 KB Output is correct
15 Correct 6 ms 9872 KB Output is correct
16 Correct 6 ms 9936 KB Output is correct
17 Correct 6 ms 9936 KB Output is correct
18 Correct 6 ms 9852 KB Output is correct
19 Correct 6 ms 9936 KB Output is correct
20 Correct 7 ms 9936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9680 KB Output is correct
2 Correct 5 ms 9720 KB Output is correct
3 Correct 6 ms 9712 KB Output is correct
4 Correct 6 ms 9808 KB Output is correct
5 Correct 93 ms 24316 KB Output is correct
6 Correct 52 ms 33480 KB Output is correct
7 Correct 89 ms 30272 KB Output is correct
8 Correct 77 ms 24724 KB Output is correct
9 Correct 97 ms 28712 KB Output is correct
10 Correct 71 ms 24672 KB Output is correct
11 Correct 7 ms 9680 KB Output is correct
12 Correct 5 ms 9680 KB Output is correct
13 Correct 5 ms 9936 KB Output is correct
14 Correct 114 ms 36916 KB Output is correct
15 Correct 130 ms 36900 KB Output is correct
16 Correct 98 ms 36828 KB Output is correct
17 Correct 124 ms 36832 KB Output is correct
18 Correct 116 ms 36972 KB Output is correct
19 Correct 109 ms 36812 KB Output is correct
20 Correct 119 ms 36844 KB Output is correct
21 Correct 14 ms 10744 KB Output is correct
22 Correct 127 ms 37088 KB Output is correct
23 Correct 121 ms 37104 KB Output is correct
24 Correct 119 ms 37180 KB Output is correct
25 Correct 118 ms 37168 KB Output is correct
26 Correct 116 ms 37092 KB Output is correct
27 Correct 124 ms 37064 KB Output is correct
28 Correct 114 ms 37188 KB Output is correct
29 Correct 122 ms 37084 KB Output is correct
30 Correct 129 ms 37164 KB Output is correct
31 Correct 151 ms 27028 KB Output is correct
32 Correct 108 ms 36848 KB Output is correct
33 Correct 207 ms 33236 KB Output is correct
34 Correct 111 ms 27576 KB Output is correct
35 Correct 178 ms 32584 KB Output is correct
36 Correct 109 ms 27640 KB Output is correct
37 Correct 187 ms 32296 KB Output is correct
38 Correct 172 ms 27468 KB Output is correct
39 Correct 125 ms 36800 KB Output is correct
40 Correct 210 ms 31120 KB Output is correct
41 Correct 6 ms 9856 KB Output is correct
42 Correct 6 ms 9936 KB Output is correct
43 Correct 8 ms 9988 KB Output is correct
44 Correct 7 ms 9860 KB Output is correct
45 Correct 6 ms 9872 KB Output is correct
46 Correct 6 ms 9936 KB Output is correct
47 Correct 6 ms 9936 KB Output is correct
48 Correct 6 ms 9852 KB Output is correct
49 Correct 6 ms 9936 KB Output is correct
50 Correct 7 ms 9936 KB Output is correct
51 Correct 163 ms 27668 KB Output is correct
52 Correct 146 ms 37192 KB Output is correct
53 Correct 184 ms 31540 KB Output is correct
54 Correct 121 ms 27808 KB Output is correct
55 Correct 181 ms 27388 KB Output is correct
56 Correct 147 ms 37072 KB Output is correct
57 Correct 193 ms 32316 KB Output is correct
58 Correct 112 ms 27716 KB Output is correct
59 Correct 159 ms 27588 KB Output is correct
60 Correct 153 ms 37132 KB Output is correct
61 Correct 217 ms 32432 KB Output is correct
62 Correct 130 ms 27548 KB Output is correct
63 Correct 156 ms 27244 KB Output is correct
64 Correct 172 ms 37152 KB Output is correct
65 Correct 253 ms 32208 KB Output is correct
66 Correct 128 ms 27812 KB Output is correct
67 Correct 162 ms 27276 KB Output is correct
68 Correct 156 ms 37156 KB Output is correct
69 Correct 204 ms 30964 KB Output is correct
70 Correct 118 ms 27920 KB Output is correct