Submission #3773322


Source Code Expand

#include <bits/stdc++.h>

using namespace std;
#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define ll long long
#define ll1 1ll
#define ONE 1ll
#define DBG 1
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define loop(n) rep(loop, (0), (n))
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using pii = pair<int, int>;
// template<class T> ostream &operator<<(ostream &os,T &t){dump(t);return os;}
template <typename T, typename S>
istream &operator>>(istream &is, pair<T, S> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> &p) {
  os << p.first << " " << p.second;
  return os;
}

template <typename T> void printvv(const vector<vector<T>> &v) {
  cerr << endl;
  rep(i, 0, v.size()) rep(j, 0, v[i].size()) {
    if (typeid(v[i][j]).name() == typeid(INF).name() and v[i][j] == INF) {
      cerr << "INF";
    } else
      cerr << v[i][j];
    cerr << (j == v[i].size() - 1 ? '\n' : ' ');
  }
  cerr << endl;
}
/*
   typedef __int128_t Int;
   std::ostream &operator<<(std::ostream &dest, __int128_t value) {
   std::ostream::sentry s(dest);
   if (s) {
   __uint128_t tmp = value < 0 ? -value : value;
   char buffer[128];
   char *d = std::end(buffer);
   do {
   --d;
 *d = "0123456789"[tmp % 10];
 tmp /= 10;
 } while (tmp != 0);
 if (value < 0) {
 --d;
 *d = '-';
 }
 int len = std::end(buffer) - d;
 if (dest.rdbuf()->sputn(d, len) != len) {
 dest.setstate(std::ios_base::badbit);
 }
 }
 return dest;
 }

 __int128 parse(string &s) {
 __int128 ret = 0;
 for (int i = 0; i < s.length(); i++)
 if ('0' <= s[i] && s[i] <= '9')
 ret = 10 * ret + s[i] - '0';
 return ret;
 }
 */

#ifndef _DEBUG
#define printvv(...)
#endif
void YES(bool f) { cout << (f ? "YES" : "NO") << endl; }
void Yes(bool f) { cout << (f ? "Yes" : "No") << endl; }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int ans = 0;
int dfs(int p, int v, int mode, vector<vector<pii>> &g) {

  vector<vector<pii>> depth(2, vector<pii>(1, pii(0, -1)));
  for (auto u : g[v]) {
    if (u.fi == p)
      continue;
    depth[u.se].eb(dfs(v, u.fi, u.se, g), u.fi);
  }
  sort(all(depth[0]), greater<pii>());
  sort(all(depth[1]), greater<pii>());
  dump(p, v, mode, depth);

  if (depth[0][0].se != depth[1][0].se) {
    chmax(ans, depth[0][0].fi + depth[1][0].fi + 1);
  } else {
    if (depth[1].size() > 1)
      chmax(ans, depth[0][0].fi + depth[1][1].fi + 1);
    if (depth[0].size() > 1)
      chmax(ans, depth[0][1].fi + depth[1][0].fi + 1);
  }

  if (depth[mode][0].se == -1)
    return 0;
  else
    return depth[mode][0].fi + 1;
}
signed main(signed argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  int N;
  cin >> N;
  vector<vector<pii>> g(N), d(N);
  loop(N - 1) {
    int a, b, t;
    cin >> a >> b >> t;
    a--, b--;
    if (t == 1) {
      g[a].eb(b, 1);
      g[b].eb(a, 0);
    } else {
      g[a].eb(b, 1);
      g[b].eb(a, 1);
      g[a].eb(b, 0);
      g[b].eb(a, 0);
    }
  }
  dfs(-1, 0, 0, g);
  dfs(-1, 0, 1, g);
  cout << ans << endl;

  return 0;
}

Submission Info

Submission Time
Task D - Longest Path
User norma
Language C++14 (GCC 5.4.1)
Score 0
Code Size 3787 Byte
Status WA
Exec Time 2107 ms
Memory 36480 KB

Judge Result

Set Name All
Score / Max Score 0 / 100
Status
AC × 4
WA × 6
TLE × 22
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 1 ms 256 KB
00-sample-01.txt AC 1 ms 256 KB
10-path-00.txt WA 1 ms 256 KB
10-path-01.txt TLE 2107 ms 36480 KB
10-path-02.txt TLE 2104 ms 3456 KB
10-path-03.txt TLE 2104 ms 7424 KB
10-path-04.txt TLE 2105 ms 13312 KB
20-path_special-00.txt AC 126 ms 34304 KB
20-path_special-01.txt TLE 2107 ms 30848 KB
20-path_special-02.txt WA 120 ms 25856 KB
30-star-00.txt WA 130 ms 16772 KB
30-star-01.txt WA 5 ms 640 KB
30-star-02.txt WA 53 ms 7864 KB
30-star-03.txt WA 89 ms 7536 KB
40-binary-00.txt TLE 2105 ms 12672 KB
40-binary-01.txt TLE 2104 ms 13568 KB
40-binary-02.txt TLE 2103 ms 512 KB
40-binary-03.txt TLE 2104 ms 9600 KB
40-binary-04.txt TLE 2103 ms 768 KB
50-random-00.txt TLE 2104 ms 14208 KB
50-random-01.txt TLE 2104 ms 4352 KB
50-random-02.txt TLE 2104 ms 7552 KB
50-random-03.txt TLE 2104 ms 5760 KB
50-random-04.txt TLE 2104 ms 6400 KB
50-random-05.txt TLE 2104 ms 11392 KB
50-random-06.txt AC 328 ms 1024 KB
50-random-07.txt TLE 2103 ms 2048 KB
50-random-08.txt TLE 2104 ms 10880 KB
50-random-09.txt TLE 2104 ms 7680 KB
50-random-10.txt TLE 2104 ms 7552 KB
50-random-11.txt TLE 2103 ms 2304 KB
50-random-12.txt TLE 2107 ms 11520 KB