답안 #537158

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
537158 2022-03-14T16:48:32 Z NaimSS 구슬과 끈 (APIO14_beads) C++14
0 / 100
5 ms 8164 KB
#include <bits/stdc++.h>
#define ld long double
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define Unique(v) sort(all(v));v.erase(unique(all(v)),v.end());
#define sz(v) ((int)v.size())
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
//#define left oooooopss
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
#define db3(x,y,z) cerr << #x<<" == "<<x<<", "<<#y<<" == "<<y<<", "<<#z<<" == "<<z<<endl;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll b,ll e,ll m){
  b%=m;
  ll ans = 1;
  for (; e; b = b * b % m, e /= 2)
      if (e & 1) ans = ans * b % m;
  return ans;
}

// debug:
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){ 
  cerr << ' ' << H; 
  dbg_out(T...); 
}
#ifdef LOCAL
#define dbg(...) cerr<<"(" << #__VA_ARGS__<<"):" , dbg_out(__VA_ARGS__) , cerr << endl
#else
#define dbg(...) 42
#endif
//
const int N = 200100;
vector<pll> g[N];
ll dp[N][2];
int n;

ll solve(int v,int pp,int p,int wPai){
  ll&x = dp[v][pp];
  if(x!=-1)return x;
  ll s1 = 0;
  vector<ll> sub;
  sub.pb(-1e9),sub.pb(-1e9);
  for(auto To : g[v])if(To.ff!=p){
    int to = To.ff,w=To.ss;
    s1 += solve(to,0,v,w);
    sub.pb(solve(to,1,v,w) - solve(to,0,v,w)+ w);
  }
  sort(all(sub),greater<ll>());
  vector<ll> soma(3);
  soma[0] = 0;
  for(int i=1;i<=2;i++){
    soma[i] = soma[i-1] + sub[i-1];
  }
  if(pp==1)x = s1 + max(soma[0],soma[2]);
  else x = max({s1 + soma[0],s1 + soma[2],s1 + soma[1] + wPai});
  return x;
}

int32_t main(){
    fastio;
    ms(dp,-1);
    cin >> n;
    ll tot=0;
    rep(i,1,n){
      int a,b,w;
      cin >> a >> b >> w;
      g[a].pb(pll(b,w));
      g[b].pb(pll(a,w));
      tot+=w;
    }
    dbg(tot);
    cout << solve(1,1,-1,1) << endl;
    // math -> gcd it all
    // Did u check N=1? Did you switch N,M?
}

Compilation message

beads.cpp: In function 'int32_t main()':
beads.cpp:50:18: warning: statement has no effect [-Wunused-value]
   50 | #define dbg(...) 42
      |                  ^~
beads.cpp:92:5: note: in expansion of macro 'dbg'
   92 |     dbg(tot);
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8164 KB Output is correct
2 Correct 5 ms 8160 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8160 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Incorrect 4 ms 8148 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8164 KB Output is correct
2 Correct 5 ms 8160 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8160 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Incorrect 4 ms 8148 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8164 KB Output is correct
2 Correct 5 ms 8160 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8160 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Incorrect 4 ms 8148 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8164 KB Output is correct
2 Correct 5 ms 8160 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8160 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Incorrect 4 ms 8148 KB Output isn't correct
7 Halted 0 ms 0 KB -