Submission #375871

#TimeUsernameProblemLanguageResultExecution timeMemory
375871ntabc05101Training (IOI07_training)C++14
100 / 100
19 ms4844 KiB
#ifndef LOCAL #define NDEBUG 1 #endif // LOCAL #include<bits/stdc++.h> #define taskname "" const int max_n=1000; const int max_m=5000; const int max_deg=10; int n, m; std::vector<int> adjList[5+max_n]; bool depth[5+max_n]; int start[5+max_n], finish[5+max_n]; int degree[5+max_n]; int timer; std::pair<int, int> parent[5+max_n]; int dp[5+max_n][1<<max_deg+5]; int result; struct Road { int x, y, w, lca; bool operator < (Road const& other) { return finish[lca]<finish[other.lca]; }; } r[5+max_m]; void dfs(int vertex) { start[vertex]=++timer; for (auto& to: adjList[vertex]) { if (to!=parent[vertex].first) { depth[to]=depth[vertex]^1; parent[to]={vertex, 1<<degree[vertex]++}; dfs(to); } } finish[vertex]=++timer; } bool is_par(int x, int y) { return start[x]<=start[y] and finish[x]>=finish[y]; } int get_lca(int x, int y) { while (!is_par(x, y)) x=parent[x].first; return x; } int main() { if (fopen(taskname".inp", "r")) { freopen(taskname".inp", "r", stdin); freopen(taskname".out", "w", stdout); } else if (fopen(taskname".in", "r")) { freopen(taskname".in", "r", stdin); freopen(taskname".out", "w", stdout); } std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cin>>n>>m; result=0; for (int i=0; i<m; ++i) { std::cin>>r[i].x>>r[i].y>>r[i].w; if (r[i].w==0) { adjList[r[i].x].push_back(r[i].y); adjList[r[i].y].push_back(r[i].x); } result+=r[i].w; } //std::cout<<result<<" "; dfs(1); for (int i=0; i<m; ++i) { r[i].lca=get_lca(r[i].x, r[i].y); } /*for (int i=0; i<m; ++i) { std::cout<<r[i].lca<<" "; }*/ std::sort(r, r+m); for (int i=0; i<m; ++i) { if (r[i].w>0 and depth[r[i].x]^depth[r[i].y]) { continue; } //std::cout<<r[i].x<<" "<<r[i].y<<" "<<r[i].w<<"\n"; int tot=r[i].w; std::pair<int, int> A, B; for (A={r[i].x, 0}; A.first!=r[i].lca; A=parent[A.first]) { tot+=dp[A.first][A.second]; } for (B={r[i].y, 0}; B.first!=r[i].lca; B=parent[B.first]) { tot+=dp[B.first][B.second]; } for (int mask=(1<<degree[r[i].lca])-1; mask>=0; --mask) { if (mask&A.second or mask&B.second) continue; dp[r[i].lca][mask]=std::max(dp[r[i].lca][mask], tot+dp[r[i].lca][mask|A.second|B.second]); } } result-=dp[1][0]; std::cout<<result<<"\n"; return 0; }

Compilation message (stderr)

training.cpp:19:27: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   19 | int dp[5+max_n][1<<max_deg+5];
      |                    ~~~~~~~^~
training.cpp: In function 'int main()':
training.cpp:53:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   53 |                 freopen(taskname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
training.cpp:54:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   54 |                 freopen(taskname".out", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
training.cpp:57:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   57 |                 freopen(taskname".in", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
training.cpp:58:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   58 |                 freopen(taskname".out", "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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...