Submission #1024908
Source Code Expand
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
using namespace std;
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int
#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))
typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;
typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int const MOD = 1000000007;
namespace std {
bool operator<(const P& a, const P& b) {
return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
}
// 移動元と行先と辺のコストを記録する構造体
struct Edge {
int from, to, cost;
Edge(int s, int d) : to(s), cost(d) {}
Edge(int f, int s, int d) : from(f), to(s), cost(d) {}
bool operator<(const Edge &e) const {
return cost < e.cost;
}
bool operator>(const Edge &e) const {
return cost > e.cost;
}
};
// 1つの頂点について、辺の情報を保存するために必要な vector
// Edgesのサイズは各頂点から出る辺の数に等しくなる
typedef vector<Edge> Edges;
// 辺の情報を保存する vector
// Graphのサイズは頂点の数に等しいので、 V である。
typedef vector<Edges> Graph;
// DP テーブル
// dp[i][j] :=
vector< vector<int> > dp;
// Graph, vertex, prev
int dfs(Graph &G, int v, int p) {
int res = 0;
for(auto e : G[v]) {
if(e.to == p) continue;
res = max(res, dfs(G, e.to, v));
// conditional branch
if(e.cost == 0 || e.cost == 2) {
// v -> e.to
res = max(res, dp[0][v] + dp[1][e.to]);
dp[0][v] = max(dp[0][v], dp[0][e.to] + 1);
}
if(e.cost == 1 || e.cost == 2) {
// v <- e.to
res = max(res, dp[1][v] + dp[0][e.to]);
dp[1][v] = max(dp[1][v], dp[1][e.to] + 1);
}
}
return res;
}
signed main() {
int n; cin >> n;
int a, b, t;
Graph G(n);
rep(i,0,n-1) {
cin >> a >> b >> t;
a--; b--;
// type 0: a → b
// type 1: a ← b
// type 2: a ⇔ b
if(t == 1) {
G[a].pb(Edge(b,0));
G[b].pb(Edge(a,1));
}
else {
G[a].pb(Edge(b,2));
G[b].pb(Edge(a,2));
}
}
dp.assign(2, vector<int>(n));
cout << dfs(G, 0, -1) << endl;
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Longest Path |
User |
tsutaj |
Language |
C++11 (GCC 4.9.2) |
Score |
0 |
Code Size |
3323 Byte |
Status |
WA |
Exec Time |
149 ms |
Memory |
19860 KB |
Judge Result
Set Name |
All |
Score / Max Score |
0 / 100 |
Status |
|
Set Name |
Test Cases |
All |
00-sample-00.txt, 00-sample-01.txt, 10-path-00.txt, 10-path-01.txt, 10-path-02.txt, 10-path-03.txt, 10-path-04.txt, 20-path_special-00.txt, 20-path_special-01.txt, 20-path_special-02.txt, 30-star-00.txt, 30-star-01.txt, 30-star-02.txt, 30-star-03.txt, 40-binary-00.txt, 40-binary-01.txt, 40-binary-02.txt, 40-binary-03.txt, 40-binary-04.txt, 50-random-00.txt, 50-random-01.txt, 50-random-02.txt, 50-random-03.txt, 50-random-04.txt, 50-random-05.txt, 50-random-06.txt, 50-random-07.txt, 50-random-08.txt, 50-random-09.txt, 50-random-10.txt, 50-random-11.txt, 50-random-12.txt |
Case Name |
Status |
Exec Time |
Memory |
00-sample-00.txt |
AC |
20 ms |
800 KB |
00-sample-01.txt |
AC |
18 ms |
796 KB |
10-path-00.txt |
AC |
19 ms |
796 KB |
10-path-01.txt |
WA |
143 ms |
19856 KB |
10-path-02.txt |
WA |
47 ms |
5592 KB |
10-path-03.txt |
WA |
57 ms |
6420 KB |
10-path-04.txt |
WA |
96 ms |
12184 KB |
20-path_special-00.txt |
WA |
149 ms |
19860 KB |
20-path_special-01.txt |
AC |
138 ms |
17552 KB |
20-path_special-02.txt |
WA |
137 ms |
16916 KB |
30-star-00.txt |
AC |
120 ms |
10960 KB |
30-star-01.txt |
AC |
21 ms |
928 KB |
30-star-02.txt |
AC |
61 ms |
5204 KB |
30-star-03.txt |
AC |
64 ms |
5332 KB |
40-binary-00.txt |
AC |
134 ms |
12960 KB |
40-binary-01.txt |
WA |
120 ms |
11808 KB |
40-binary-02.txt |
AC |
21 ms |
1052 KB |
40-binary-03.txt |
WA |
100 ms |
10012 KB |
40-binary-04.txt |
AC |
22 ms |
1308 KB |
50-random-00.txt |
AC |
125 ms |
11936 KB |
50-random-01.txt |
WA |
52 ms |
4596 KB |
50-random-02.txt |
AC |
83 ms |
7540 KB |
50-random-03.txt |
AC |
66 ms |
5880 KB |
50-random-04.txt |
WA |
72 ms |
6428 KB |
50-random-05.txt |
AC |
119 ms |
11296 KB |
50-random-06.txt |
WA |
24 ms |
1560 KB |
50-random-07.txt |
AC |
34 ms |
2468 KB |
50-random-08.txt |
AC |
111 ms |
10664 KB |
50-random-09.txt |
AC |
88 ms |
7716 KB |
50-random-10.txt |
WA |
84 ms |
7412 KB |
50-random-11.txt |
WA |
34 ms |
2548 KB |
50-random-12.txt |
AC |
125 ms |
11232 KB |