답안 #369392

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
369392 2021-02-21T14:16:59 Z ACmachine Tug of War (BOI15_tug) C++17
0 / 100
3000 ms 19044 KB
#include <bits/stdc++.h>
using namespace std;
 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
 
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;
 
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<str> vstr;
 
#define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
#define FORD(i,j,k,in) for(int i=(j); i >=(k);i-=in)
#define REP(i,b) FOR(i,0,b,1)
#define REPD(i,b) FORD(i,b,0,1)
#define pb push_back 
#define mp make_pair
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define rsz resize 
#define MANY_TESTS int tcase; cin >> tcase; while(tcase--)
	
const double EPS = 1e-9;
const int MOD = 1e9+7; // 998244353;
const ll INFF = 1e18;
const int INF = 1e9;
const ld PI = acos((ld)-1);
const vi dy = {1, 0, -1, 0, -1, 1, 1, -1};
const vi dx = {0, 1, 0, -1, -1, 1, -1, 1};
 
#ifdef DEBUG
#define DBG if(1)
#else
#define DBG if(0)
#endif
 
#define dbg(x) cout << "(" << #x << " : " << x << ")" << endl;
// ostreams
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "[" << par.first << ";" << par.second << "]"; return out;}
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template<class T>
ostream& operator<<(ostream& out, const vector<T> &v){ out << "[";  REP(i, v.size()) out << v[i] << ", ";  out << "]"; return out;}
// istreams
template<class T>
istream& operator>>(istream& in, vector<T> &v){ for(auto &x : v) in >> x; return in; }
template<class T, class U>
istream& operator>>(istream& in, pair<T, U> &p){ in >> p.ff >> p.ss; return in; }
//searches
template<typename T, typename U>
T bsl(T lo, T hi, U f){ hi++; T mid; while(lo < hi){ mid = (lo + hi)/2; f(mid) ? hi = mid : lo = mid+1; } return lo; }
template<typename U>
double bsld(double lo, double hi, U f, double p = 1e-9){ int r = 3 + (int)log2((hi - lo)/p); double mid; while(r--){ mid = (lo + hi)/2; f(mid) ? hi = mid : lo = mid; } return (lo + hi)/2; }
template<typename T, typename U>
T bsh(T lo, T hi, U f){ lo--; T mid; while(lo < hi){ mid = (lo + hi + 1)/2; f(mid) ? lo = mid : hi = mid-1; } return lo; }
template<typename U>
double bshd(double lo, double hi, U f, double p = 1e-9){ int r = 3+(int)log2((hi - lo)/p); double mid; while(r--){ mid = (lo + hi)/2; f(mid) ? lo = mid : hi = mid; } return (lo + hi)/2; }
// some more utility functions
template<typename T>
pair<T, int> get_min(vector<T> &v){ typename vector<T> :: iterator it = min_element(v.begin(), v.end()); return mp(*it, it - v.begin());}
template<typename T>
pair<T, int> get_max(vector<T> &v){ typename vector<T> :: iterator it = max_element(v.begin(), v.end()); return mp(*it, it - v.begin());}        
template<typename T> bool ckmin(T& a, const T& b){return b < a ? a = b , true : false;}
template<typename T> bool ckmax(T& a, const T& b){return b > a ? a = b, true : false;}
 
    
int main(){
 	ios_base::sync_with_stdio(false);
 	cin.tie(NULL); cout.tie(NULL);
	int n, k; cin >> n >> k;
    vector<vector<array<int, 3>>> g(2 * n);
    vector<array<int, 4>> edges;
    vector<int> deg(2 * n, 0);
    REP(i, 2 * n){
        int l, r, s;
        cin >> l >> r >> s;
        l--; r--;
        g[l].pb({r + n, s, i});
        g[r + n].pb({l, s, i});
        edges.pb({l, r + n, s, i});
        deg[l]++;
        deg[r + n]++;
    }
    vector<int> component_id(2 * n, -1);
    function<void(int, int)> dfs_comp = [&](int v, int id){
        component_id[v] = id;
        for(auto x : g[v]){
            if(component_id[x[0]] == -1) 
                dfs_comp(x[0], id);
        }
    };
    int curr_id = 0;
    REP(i, 2 * n){
        if(component_id[i] == -1){
            dfs_comp(i, curr_id);
            curr_id++;
        }
    }
    vector<int> vertex_count(2 * n, 0);
    vector<int> edge_count(2 * n, 0);
    REP(i, 2 * n) vertex_count[component_id[i]]++;
    REP(i, edges.size()) edge_count[component_id[edges[i][0]]]++;
    bool can = true;
    REP(i, 2 * n){
        if(vertex_count[i] == edge_count[i] - 1) 
            can = false;
    }
    if(!can){
        cout << "NO" << "\n";
        return 0;
    }
    int difference = 0; // rsum - lsum
    set<array<int, 4>> used_edges;
    queue<int> q;
    vector<bool> processed(2 * n, false);
    REP(i, 2 * n){
        if(deg[i] == 1) 
            q.push(i);
    } 
    while(!q.empty()){
        int v = q.front();
        q.pop();
        for(auto x : g[v]){
            int sr = v; 
            int ds = x[0];
            int w = x[1];
            int id = x[2];
            if(sr > ds) swap(sr, ds);
            if(used_edges.find({sr, ds, w, id}) == used_edges.end()){
                used_edges.insert({sr, ds, w, id});
                processed[v] = true;
                if(v >= n) difference += w;
                else difference -= w;
                deg[x[0]]--;
                if(deg[x[0]] == 1){
                    q.push(x[0]);
                }
            }
        }
    }
    vector<int> knapsack_values;
    function<void(int, int&, int&)> dfs = [&](int v, int& A, int& B){
        processed[v] =true;
        for(auto x : g[v]){
            int sr = v;
            int ds = x[0];
            int w = x[1];
            int id = x[2];
            if(sr > ds) swap(sr, ds);
            if(used_edges.find({sr, ds, w, id}) == used_edges.end()){
                used_edges.insert({sr, ds, w, id});
                if(v >= n) A += w;
                else A -= w;
                if(x[0] >= n) B += w;
                else B -= w;
                dfs(x[0], A, B);
            }
        }
    };
    REP(i, 2 * n){
        if(!processed[i]){
            //cycle -> get 2 values, A and B
            int A = 0, B = 0;
            dfs(i, A, B);
            if(A > B) swap(A, B);
            difference += A; 
            knapsack_values.pb(B - A);
        }
    }
    vector<int> new_knapsack_values;
    vector<int> cnt(2e6, 0);
    for(auto val : knapsack_values) cnt[val]++;
    REP(i, 2e6){
        if(cnt[i] == 0) continue;
        int df = (cnt[i] - 1) / 2;
        cnt[i] -= df * 2;
        cnt[2 * i] += df;
    }
    REP(i, 2e6){
        REP(j, cnt[i])
            new_knapsack_values.pb(i);
    }
    const int mx = 2e6+5;
	vector<int> dp(mx, 0);
    dp[0] = 1;
    for(int val : new_knapsack_values){
        REPD(i, mx - 2){
            if(i - val < 0) break;
            if(dp[i - val]) dp[i] = 1; 
        }
    }
    REP(i, mx){
        if(dp[i] == 1 && abs(difference + i) <= k){
            cout << "YES" << "\n";
            return 0;
        }
    }
    cout << "NO" << "\n";
    return 0;
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:25:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 | #define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
      |                                        ^
tug.cpp:27:18: note: in expansion of macro 'FOR'
   27 | #define REP(i,b) FOR(i,0,b,1)
      |                  ^~~
tug.cpp:119:5: note: in expansion of macro 'REP'
  119 |     REP(i, edges.size()) edge_count[component_id[edges[i][0]]]++;
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 15980 KB Output is correct
2 Correct 40 ms 15980 KB Output is correct
3 Correct 43 ms 15980 KB Output is correct
4 Correct 40 ms 15980 KB Output is correct
5 Correct 39 ms 15980 KB Output is correct
6 Correct 39 ms 15980 KB Output is correct
7 Correct 40 ms 15980 KB Output is correct
8 Correct 40 ms 15980 KB Output is correct
9 Correct 41 ms 16108 KB Output is correct
10 Correct 43 ms 16052 KB Output is correct
11 Correct 40 ms 15980 KB Output is correct
12 Correct 41 ms 15980 KB Output is correct
13 Correct 43 ms 15980 KB Output is correct
14 Correct 40 ms 15980 KB Output is correct
15 Correct 40 ms 15980 KB Output is correct
16 Correct 42 ms 15980 KB Output is correct
17 Correct 41 ms 15980 KB Output is correct
18 Correct 39 ms 15980 KB Output is correct
19 Correct 40 ms 15980 KB Output is correct
20 Correct 40 ms 15980 KB Output is correct
21 Incorrect 27 ms 15980 KB Output isn't correct
22 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 15980 KB Output is correct
2 Correct 40 ms 15980 KB Output is correct
3 Correct 43 ms 15980 KB Output is correct
4 Correct 40 ms 15980 KB Output is correct
5 Correct 39 ms 15980 KB Output is correct
6 Correct 39 ms 15980 KB Output is correct
7 Correct 40 ms 15980 KB Output is correct
8 Correct 40 ms 15980 KB Output is correct
9 Correct 41 ms 16108 KB Output is correct
10 Correct 43 ms 16052 KB Output is correct
11 Correct 40 ms 15980 KB Output is correct
12 Correct 41 ms 15980 KB Output is correct
13 Correct 43 ms 15980 KB Output is correct
14 Correct 40 ms 15980 KB Output is correct
15 Correct 40 ms 15980 KB Output is correct
16 Correct 42 ms 15980 KB Output is correct
17 Correct 41 ms 15980 KB Output is correct
18 Correct 39 ms 15980 KB Output is correct
19 Correct 40 ms 15980 KB Output is correct
20 Correct 40 ms 15980 KB Output is correct
21 Incorrect 27 ms 15980 KB Output isn't correct
22 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3068 ms 19044 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 15980 KB Output is correct
2 Correct 40 ms 15980 KB Output is correct
3 Correct 43 ms 15980 KB Output is correct
4 Correct 40 ms 15980 KB Output is correct
5 Correct 39 ms 15980 KB Output is correct
6 Correct 39 ms 15980 KB Output is correct
7 Correct 40 ms 15980 KB Output is correct
8 Correct 40 ms 15980 KB Output is correct
9 Correct 41 ms 16108 KB Output is correct
10 Correct 43 ms 16052 KB Output is correct
11 Correct 40 ms 15980 KB Output is correct
12 Correct 41 ms 15980 KB Output is correct
13 Correct 43 ms 15980 KB Output is correct
14 Correct 40 ms 15980 KB Output is correct
15 Correct 40 ms 15980 KB Output is correct
16 Correct 42 ms 15980 KB Output is correct
17 Correct 41 ms 15980 KB Output is correct
18 Correct 39 ms 15980 KB Output is correct
19 Correct 40 ms 15980 KB Output is correct
20 Correct 40 ms 15980 KB Output is correct
21 Incorrect 27 ms 15980 KB Output isn't correct
22 Halted 0 ms 0 KB -