Submission #375871

# Submission time Handle Problem Language Result Execution time Memory
375871 2021-03-10T06:56:33 Z ntabc05101 Training (IOI07_training) C++14
100 / 100
19 ms 4844 KB
#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

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 time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 748 KB Output is correct
2 Correct 1 ms 620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 4840 KB Output is correct
2 Correct 11 ms 4844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 620 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 876 KB Output is correct
2 Correct 1 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1260 KB Output is correct
2 Correct 2 ms 1004 KB Output is correct
3 Correct 4 ms 1644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2412 KB Output is correct
2 Correct 6 ms 1772 KB Output is correct
3 Correct 4 ms 1644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1004 KB Output is correct
2 Correct 3 ms 1772 KB Output is correct
3 Correct 19 ms 4716 KB Output is correct
4 Correct 4 ms 1900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 2412 KB Output is correct
2 Correct 18 ms 4716 KB Output is correct
3 Correct 9 ms 2156 KB Output is correct
4 Correct 8 ms 1900 KB Output is correct