Submission #949854

# Submission time Handle Problem Language Result Execution time Memory
949854 2024-03-19T18:59:59 Z Rifal Election Campaign (JOI15_election_campaign) C++14
10 / 100
74 ms 26448 KB
#include <bits/stdc++.h>
#include <fstream>
#define endl '\n'
#define mod 1000000007
#define INF 1000000000
#define INF2 1000000000000000000
///#define cin fin
///#define cout fout
using namespace std;
double const EPS = 1e-14;
typedef long long ll;
///ofstream fout("herding.out");
///ifstream fin("herding.in");
const int N = 1e5 + 5;
vector<int> v[N];
pair<int,int> inter[N];
ll ans = 0;
int dep[N];
int lca[N][31];
int tim = 1;
struct Plan {
    int a, b; ll c;
};
void dfs(int s, int p) {
    for(int i = 1; (1<<i) <= dep[s]; i++) {
        lca[s][i] = lca[lca[s][i-1]][i-1];
    }
    inter[s].first = tim; tim++;
    for(auto i : v[s]) {
        if(i != p) {
            dep[i] = dep[s]+1;
            lca[i][0] = s;
            dfs(i,s);
        }
    }
    inter[s].second = tim; tim++;
}
int fin(int x, int y) {
    if(dep[x] < dep[y]) swap(x,y);
    int pow2 = 0;
    while(dep[x]-dep[y] > 0) {
        if(((dep[x]-dep[y])&(1<<pow2)) > 0) {
            x = lca[x][pow2];
        }
        pow2++;
    }
    while(x != y) {
        pow2 = 30;
        while(lca[x][pow2] == lca[y][pow2] && pow2 != 0) pow2--;
        x = lca[x][pow2];
        y = lca[y][pow2];
    }
    return x;
}
bool check(pair<int,int> x, pair<int,int> y) {
    int nod1 = x.first, nod2 = x.second, nod3 = y.first, nod4 = y.second;
    if(inter[nod1].first <= inter[nod3].first && inter[nod1].second >= inter[nod3].second && inter[nod2].first >= inter[nod3].first && inter[nod2].second <= inter[nod3].second) return true;
    else if(inter[nod1].first <= inter[nod4].first && inter[nod1].second >= inter[nod4].second && inter[nod2].first >= inter[nod4].first && inter[nod2].second <= inter[nod4].second) return true;
    else if(inter[nod3].first <= inter[nod1].first && inter[nod3].second >= inter[nod1].second && inter[nod4].first >= inter[nod1].first && inter[nod4].second <= inter[nod1].second) return true;
    else if(inter[nod3].first <= inter[nod2].first && inter[nod3].second >= inter[nod2].second && inter[nod4].first >= inter[nod2].first && inter[nod4].second <= inter[nod2].second) return true;
    return false;
}
int main()
{
    ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
    int n; cin >> n;
    for(int i = 0; i < n-1; i++) {
        int a, b; cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    int m; cin >> m;
    Plan arr[m];
    for(int i = 0; i < m; i++) {
        cin >> arr[i].a >> arr[i].b >> arr[i].c;
    }
    dfs(1,0);
    for(int i = 0; i < (1<<m); i++) {
        vector<int> cur;
        ll sum2 = 0;
        for(int j = 0; j < m; j++) {
            if((i&(1<<j)) > 0) {
                cur.push_back(j);
                sum2 += arr[j].c;
            }
        }
        bool ok = true;
        ll sum = 0;
        for(int j = 0; j < cur.size(); j++) {
            int nod1 = fin(arr[cur[j]].a,arr[cur[j]].b);
            for(int z = j+1; z < cur.size(); z++) {
                int nod2 = fin(arr[cur[z]].a,arr[cur[z]].b);
                if(check({nod1,arr[cur[j]].a},{nod2,arr[cur[z]].a}) || check({nod1,arr[cur[j]].b},{nod2,arr[cur[z]].a}) || check({nod1,arr[cur[j]].a},{nod2,arr[cur[z]].b}) || check({nod1,arr[cur[j]].b},{nod2,arr[cur[z]].b})) {
                    ok = false; break;
                }
            }
            if(ok) sum += arr[cur[j]].c;
            else break;
        }
        if(ok) {
          //  cout << i << ' ' << sum << 'd' << endl;
            ans = max(ans,sum);
        }
    }
    cout << ans << endl;
    return 0;
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:89:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |         for(int j = 0; j < cur.size(); j++) {
      |                        ~~^~~~~~~~~~~~
election_campaign.cpp:91:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |             for(int z = j+1; z < cur.size(); z++) {
      |                              ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3416 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 13 ms 3676 KB Output is correct
5 Correct 53 ms 20308 KB Output is correct
6 Correct 45 ms 26448 KB Output is correct
7 Correct 74 ms 24468 KB Output is correct
8 Correct 36 ms 20764 KB Output is correct
9 Correct 58 ms 23180 KB Output is correct
10 Correct 41 ms 20988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3420 KB Output is correct
2 Incorrect 4 ms 3572 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3420 KB Output is correct
2 Incorrect 4 ms 3572 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 72 ms 23184 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3416 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 13 ms 3676 KB Output is correct
5 Correct 53 ms 20308 KB Output is correct
6 Correct 45 ms 26448 KB Output is correct
7 Correct 74 ms 24468 KB Output is correct
8 Correct 36 ms 20764 KB Output is correct
9 Correct 58 ms 23180 KB Output is correct
10 Correct 41 ms 20988 KB Output is correct
11 Incorrect 3 ms 3676 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3416 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 13 ms 3676 KB Output is correct
5 Correct 53 ms 20308 KB Output is correct
6 Correct 45 ms 26448 KB Output is correct
7 Correct 74 ms 24468 KB Output is correct
8 Correct 36 ms 20764 KB Output is correct
9 Correct 58 ms 23180 KB Output is correct
10 Correct 41 ms 20988 KB Output is correct
11 Correct 2 ms 3420 KB Output is correct
12 Incorrect 4 ms 3572 KB Output isn't correct
13 Halted 0 ms 0 KB -