Submission #1283144

#TimeUsernameProblemLanguageResultExecution timeMemory
1283144vuquangsangElection Campaign (JOI15_election_campaign)C++20
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
using namespace std;

#define     el "\n"
#define     FOR(i,a,b) for(int i = (a), _b = (b); i <= _b; i++)
#define     FORD(i,a,b) for(int i = (a), _b = (b); i >= _b; i--)
#define     pb push_back
#define     fi first
#define     se second
#define     all(x) x.begin(),x.end()
#define     lg(x) __lg(x)
#define     alla(a,n) a+1,a+n+1
#define     ll long long


template <class T> bool maxi(T &x, T y) { if(x < y) { x = y ; return true ;} return false;}
template <class T> bool mini(T &x, T y) { if(x > y) { x = y ; return true ;} return false;}

const int N = 1e5 + 2;

struct Edge
{
    int x, y, w;
} E[N];

int n, m;
vector<int> adj[N];

void inp()
{
    cin >> n;
    FOR(i, 1, n - 1) {
        int x, y; cin >> x >> y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    cin >> m;
    FOR(i, 1, m) {
        int x, y, w;
        cin >> x >> y >> w;
        E[i] = {x, y, w};
    }
}

/* Try your best
    No regrets */

namespace subtask_1
{

    int h[N], up[N][21];
    int par[N];

    void dfs(int u, int p)
    {
        up[u][0] = p;
        FOR(j, 1, lg(n)) up[u][j] = up[up[u][j - 1]][j - 1];

        for(int v : adj[u]) if(v != p) {
            h[v] = h[u] + 1;
            par[v] = u;
            dfs(v, u);
        }
    }
    int LCA(int u, int v)
    {
        if(h[u] < h[v]) swap(u, v);
        FORD(j, lg(n), 0) if(h[up[u][j]] >= h[v]) u = up[u][j];
        if(u == v) return u;
        FORD(j, lg(n), 0) if(up[u][j] != up[v][j]) {
            u = up[u][j];
            v = up[v][j];
        }
        return up[u][0];
    }

    int Val[N];
    void update(int x, int y)
    {
        int p = LCA(x, y);
        while(x != p) {
            Val[x]++;
            x = par[x];
        }
        while(y != p) {
            Val[y]++;
            y = par[y];
        }
        Val[p]++;
    }
    void slv()
    {
        dfs(1, 0);

        ll ans = 0;
        FOR(msk, 1, (1 << m) - 1) {
            FOR(i, 1, n) Val[i] = 0;

            ll res = 0;
            FOR(i, 0, m - 1) if(msk >> i & 1) {
                int x = E[i + 1].x, y = E[i + 1].y, w = E[i + 1].w;
                update(x, y);
                res += w;
            }

            bool oki = 1;
            FOR(i, 1, n) if(Val[i] > 1) oki = 0;
            if(oki) maxi(ans, res);
        }
        cout << ans;
    }
}

namespace subtask_23
{

    void slv()
    {
        sort(E + 1, E + m + 1,[&](Edge a, Edge b) {
            return a.x < b.y;
        });

        dp[1] = E[1].w;

        FOR(i, 2, m) {
            dp[i] = E[i].w;
            FOR(j, 1, i - 1) if(E[j].y < E[i].x) maxi(dp[i], dp[j] + E[i].w);
        }
        cout << *max_element(dp + 1, dp + m + 1);
    }
}
/* Code slowly, think carefully */

main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    #define __Azul__ "election_campaign"
    if(fopen(__Azul__".inp", "r")) {
        freopen(__Azul__".inp", "r", stdin);
        freopen(__Azul__".out", "w", stdout);
    }

    bool qs = 0;

    int T = 1;
    if(qs) cin >> T;
    while(T--) {
        inp();
        if(m <= 15) subtask_1 :: slv();
        else subtask_2 :: slv();
    }

    cerr << "\nTime" << 0.001 * clock() << "s "; return 0;


}





Compilation message (stderr)

election_campaign.cpp: In function 'void subtask_23::slv()':
election_campaign.cpp:123:9: error: 'dp' was not declared in this scope; did you mean 'dup'?
  123 |         dp[1] = E[1].w;
      |         ^~
      |         dup
election_campaign.cpp: At global scope:
election_campaign.cpp:134:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  134 | main()
      | ^~~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:151:14: error: 'subtask_2' has not been declared
  151 |         else subtask_2 :: slv();
      |              ^~~~~~~~~
election_campaign.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(__Azul__".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.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(__Azul__".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~