QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#181420#2169. 'S No ProblemAlleks_BWA 2ms6888kbC++142.4kb2023-09-16 18:40:302023-09-16 18:40:31

Judging History

你现在查看的是最新测评结果

  • [2023-09-16 18:40:31]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:6888kb
  • [2023-09-16 18:40:30]
  • 提交

answer

#include <bits/stdc++.h>
#define L 100005
using namespace std;

int n;
vector <pair <int, int>> G[L];
int root = 1, pathLength, maxPath, node1, node2, node3, diam1, diam2, sum;
int lev[L], t[L];
bool vis[L];
map <pair <int, int>, bool> mp;

void init() {
  for (int i = 1; i <= n; i++)
    vis[i] = false;
  pathLength = 0;
  maxPath = -L;
}

void switchEdge(int x, int y) {
  mp[{x, y}] = mp[{y, x}] = true;
}

void switchChain(int x, int y) {
  if (lev[x] < lev[y])
    swap(x, y);
  while (lev[x] > lev[y]) {
    switchEdge(x, t[x]);
    x = t[x];
  }
  while (x != y) {
    switchEdge(x, t[x]);
    switchEdge(y, t[y]);
    x = t[x];
    y = t[y];
  }
}

void DFS_1(int node) {
  vis[node] = true;
  if (maxPath < pathLength) {
    maxPath = pathLength;
    node1 = node;
  }
  for (auto it : G[node]) {
    if (!vis[it.first]) {
      lev[it.first] = lev[node] + 1;
      t[it.first] = node;
      pathLength += it.second;
      DFS_1(it.first);
      pathLength -= it.second;
    }
  }
}

void DFS_2(int node) {
  vis[node] = true;
  if (maxPath < pathLength) {
    maxPath = pathLength;
    node2 = node;
  }
  for (auto it : G[node]) {
    if (!vis[it.first]) {
      pathLength += it.second;
      DFS_2(it.first);
      pathLength -= it.second;
    }
  }
}

void DFS_3(int node) {
  vis[node] = true;
  if (maxPath < pathLength) {
    maxPath = pathLength;
    node3 = node;
  }
  for (auto it : G[node]) {
    if (!vis[it.first]) {
      int cost = it.second;
      if (mp[{node, it.first}])
        cost = -cost;
      pathLength += cost;
      DFS_3(it.first);
      pathLength -= cost;
    }
  }
}

void DFS_4(int node) {
  vis[node] = true;
  maxPath = max(maxPath, pathLength);
  for (auto it : G[node]) {
    if (!vis[it.first]) {
      int cost = it.second;
      if (mp[{node, it.first}])
        cost = -cost;
      pathLength += cost;
      DFS_4(it.first);
      pathLength -= cost;
    }
  }
}

int main(){
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
    sum += c;
  }

  init();
  DFS_1(root);

  init();
  DFS_2(node1);
  diam1 = maxPath;

  switchChain(node1, node2);

  init();
  DFS_3(root);

  init();
  DFS_4(node3);
  diam2 = maxPath;

  cout << sum * 2 - diam1 - diam2 << "\n";
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 6052kb

input:

5
1 2 1
1 3 2
1 4 3
1 5 4

output:

10

result:

ok single line: '10'

Test #2:

score: 0
Accepted
time: 0ms
memory: 5900kb

input:

6
6 2 1
4 6 1
3 1 1
1 5 1
1 6 10

output:

15

result:

ok single line: '15'

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 6888kb

input:

6
1 3 1
3 2 1
3 4 10
5 4 1
4 6 1

output:

16

result:

wrong answer 1st lines differ - expected: '15', found: '16'