#include <bits/stdc++.h>
using namespace std;
// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF 1e18
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// input
template<class T> void IN(T& x) {cin >> x;}
template<class H, class... T> void IN(H& h, T&... t) {IN(h); IN(t...); }
// output
template<class T1, class T2> void OUT(const pair<T1,T2>& x);
template<class T> void OUT(const vector<T>& x);
template<class T> void OUT(const T& x) {cout << x;}
template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); }
template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);}
template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);}
template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); }
template<class H> void OUTLS(const H& h) {OUTL(h); }
template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); }
// dp
template<class T> bool ckmin(T&a, T&b) { bool bl = a > b; a = min(a,b); return bl;}
template<class T> bool ckmax(T&a, T&b) { bool bl = a < b; a = max(a,b); return bl;}
void program();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
program();
}
//===================//
// begin program //
//===================//
const int MX = 2050;
const int N = 5005;
int n, m;
int a, b, c;
vi adj[MX];
vi chl[MX];
vi path[MX];
vi pathsOnVertex[MX];
int childrenBitMask[MX][N];
int par[MX];
int depth[MX];
int rootPath[N];
int childNumber[MX];
viii unPaved;
int totUnPaved;
void dfsChl(int u, int p) {
par[u] = p;
childNumber[u] = -1;
depth[u] = p == -1 ? 0 : depth[p] + 1;
int cc = 0;
FOR(v,adj[u]) if(v != p) {
dfsChl(v,u);
childNumber[v] = cc++;
chl[u].pb(v);
}
}
vi dfsPath(int u, int target, int p) {
if(u == target) return {u};
FOR(v,adj[u]) if(v != p) {
vi res = dfsPath(v,target,u);
if(res.empty()) continue;
res.pb(u);
return res;
}
return {};
}
int dp[MX][MX];
int dpPath[N];
int getAns(int u, int bm);
int getAnsPath(int pi) {
if(dpPath[pi] != -1) return dpPath[pi];
int _, res;
tie(_,_,res) = unPaved[pi];
FOR(u,path[pi]) {
if(u == rootPath[pi]) continue;
res += getAns(u,childrenBitMask[u][pi]);
}
return dpPath[pi] = res;
}
int getAns(int u, int bm) {
if(dp[u][bm] != -1) return dp[u][bm];
int res = 0;
RE(i,chl[u].size())
if((bm&(1<<i)) == 0)
res += getAns(chl[u][i], 0);
FOR(pi,pathsOnVertex[u]) {
if(bm&childrenBitMask[u][pi]) continue;
int nRes = getAnsPath(pi);
nRes += getAns(u,bm | childrenBitMask[u][pi]);
res = max(res, nRes);
}
return dp[u][bm] = res;
}
void program() {
IN(n,m);
RE(i,m) {
IN(a,b,c); a--; b--;
if(c == 0) adj[a].pb(b), adj[b].pb(a);
else unPaved.pb({a,b,c});
totUnPaved += c;
}
dfsChl(0,-1);
RE(i,unPaved.size()) {
int u, v, d;
tie(u,v,d) = unPaved[i];
path[i] = dfsPath(u,v,-1);
if((path[i].size()%2) == 0) continue;
int highest = u;
FOR(u,path[i]) if(depth[u] < depth[highest]) highest = u;
pathsOnVertex[highest].pb(i);
rootPath[i] = highest;
FOR(u,path[i]) if(par[u] != -1) childrenBitMask[par[u]][i] |= (1<<childNumber[u]);
}
memset(dp,-1,sizeof(dp));
memset(dpPath,-1,sizeof(dpPath));
OUTL(totUnPaved - getAns(0,0));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16984 KB |
Output is correct |
2 |
Correct |
9 ms |
16972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
17168 KB |
Output is correct |
2 |
Correct |
9 ms |
17228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
32688 KB |
Output is correct |
2 |
Runtime error |
73 ms |
33328 KB |
Execution killed with signal 11 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16972 KB |
Output is correct |
2 |
Correct |
9 ms |
16972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16972 KB |
Output is correct |
2 |
Correct |
9 ms |
16972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
17100 KB |
Output is correct |
2 |
Correct |
9 ms |
17128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
17612 KB |
Output is correct |
2 |
Correct |
13 ms |
17456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
18500 KB |
Output is correct |
2 |
Correct |
19 ms |
18364 KB |
Output is correct |
3 |
Correct |
23 ms |
20136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
35 ms |
10444 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
17748 KB |
Output is correct |
2 |
Correct |
24 ms |
21024 KB |
Output is correct |
3 |
Runtime error |
62 ms |
31536 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
39 ms |
12452 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |