답안 #949053

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
949053 2024-03-18T21:37:28 Z Rifal Election Campaign (JOI15_election_campaign) C++14
0 / 100
52 ms 23424 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[nod2].first >= inter[nod3].first) return true;
    else if(inter[nod1].first <= inter[nod4].first && inter[nod2].first >= inter[nod4].first) return true;
    else if(inter[nod3].first <= inter[nod1].first && inter[nod4].first >= inter[nod1].first) return true;
    else if(inter[nod3].first <= inter[nod2].first && inter[nod4].first >= inter[nod2].first) 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;
        for(int j = 0; j < m; j++) {
            if((i&(1<<j)) > 0) {
                cur.push_back(j);
            }
        }
        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:87:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |         for(int j = 0; j < cur.size(); j++) {
      |                        ~~^~~~~~~~~~~~
election_campaign.cpp:89:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |             for(int z = j+1; z < cur.size(); z++) {
      |                              ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3416 KB Output is correct
2 Incorrect 2 ms 3416 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3420 KB Output is correct
2 Incorrect 2 ms 3420 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3420 KB Output is correct
2 Incorrect 2 ms 3420 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 52 ms 23424 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3416 KB Output is correct
2 Incorrect 2 ms 3416 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3416 KB Output is correct
2 Incorrect 2 ms 3416 KB Output isn't correct
3 Halted 0 ms 0 KB -