제출 #992399

#제출 시각아이디문제언어결과실행 시간메모리
992399yeediot구슬과 끈 (APIO14_beads)C++17
100 / 100
84 ms27012 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define F first #define S second #define all(x) x.begin(),x.end() #define pii pair<int,int> #define pb push_back #define sz(x) (int)(x.size()) #define chmin(x,y) x=min(x,y) #define chmax(x,y) x=max(x,y) #define vi vector<int> #define vp vector<pii> #define vvi vector<vi> #define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()) #define __lg(x) 63-__builtin_clzll(x) #define pow2(x) (1LL<<x) void __print(int x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } #ifdef local void CHECK(); void setio(){ freopen("/Users/iantsai/cpp/input.txt","r",stdin); freopen("/Users/iantsai/cpp/output.txt","w",stdout); } #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else void setio(){} #define debug(x...) #endif #define TOI_is_so_de ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);setio(); int n; const int mxn = 2e5+5; const int inf = -1e18; vector<pii>adj[mxn]; int dp[mxn], ans = 0; pii dp2[mxn][2]; void dfs(int v,int pa){ int sum = 0; for(auto [u, d] : adj[v]){ if(u == pa) continue; dfs(u, v); dp[v] += max(dp[u], dp2[u][0].F+d); } for(auto [u, d] : adj[v]){ if(u == pa) continue; pii p = {dp[v]-max(dp[u], dp2[u][0].F+d)+d+dp[u], u}; auto &x=dp2[v]; if(p.F >= x[0].F){ x[1] = x[0]; x[0] = p; } else if(p.F > x[1].F){ x[1] = p; } } if(sz(adj[v])==1 and pa!=v){ dp2[v][0]={inf,inf}; dp2[v][1]={inf,inf}; } } void cal(int v,int pa){ chmax(ans, dp[v]); for(auto [u, d] : adj[v]){ if(u == pa) continue; auto p = dp2[v]; auto x = dp[v]; dp[v] -= max(dp[u], dp2[u][0].F+d); auto p1 = dp2[v][0]; if(p1.S == u) p1 = dp2[v][1]; p1.F -= max(dp[u], dp2[u][0].F+d); dp[u] += max(dp[v], p1.F+d); dp2[u][0].F += max(dp[v], p1.F+d); dp2[u][1].F += max(dp[v], p1.F+d); pii p2 = {dp[u]-max(dp[v], p1.F+d)+d+dp[v], v}; if(p2.F >= dp2[u][0].F){ dp2[u][1] = dp2[u][0]; dp2[u][0] = p2; } else if(p2.F >= dp2[u][1].F){ dp2[u][1] = p2; } cal(u,v); dp[v] = x; dp2[v][0]=p[0]; dp2[v][1]=p[1]; } } signed main(){ TOI_is_so_de; cin >> n; for(int i=1;i<n;i++){ int a, b, c; cin >> a >> b >> c; adj[a].pb({b,c}); adj[b].pb({a,c}); } for(int i=1;i<=n;i++){ for(int j=0;j<2;j++){ dp2[i][j]={inf,inf}; } } dfs(1,1); cal(1,1); cout << ans << '\n'; #ifdef local CHECK(); #endif } /* */ #ifdef local void CHECK(){ cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n"; function<bool(string,string)> compareFiles = [](string p1, string p2)->bool { std::ifstream file1(p1); std::ifstream file2(p2); if (!file1.is_open() || !file2.is_open())return false; std::string line1, line2; while (getline(file1, line1) && getline(file2, line2)) { if (line1 != line2)return false; } int cnta=0,cntb=0; while(getline(file1,line1))cnta++; while(getline(file2,line2))cntb++; return cntb-cnta<=1; }; bool check = compareFiles("output.txt","expected.txt"); if (check) cerr<<"ACCEPTED\n"; else cerr<<"WRONG ANSWER!\n"; } #else #endif

컴파일 시 표준 에러 (stderr) 메시지

beads.cpp: In function 'void dfs(long long int, long long int)':
beads.cpp:57:9: warning: unused variable 'sum' [-Wunused-variable]
   57 |     int sum = 0;
      |         ^~~
beads.cpp: In function 'void setIO(std::string)':
beads.cpp:35:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...