QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#181437 | #2169. 'S No Problem | Alleks_B | WA | 1ms | 6444kb | C++14 | 2.4kb | 2023-09-16 18:54:14 | 2023-09-16 18:54:15 |
Judging History
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 (node != root && 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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 6000kb
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: 1ms
memory: 6008kb
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: 0
Accepted
time: 1ms
memory: 5984kb
input:
6 1 3 1 3 2 1 3 4 10 5 4 1 4 6 1
output:
15
result:
ok single line: '15'
Test #4:
score: 0
Accepted
time: 1ms
memory: 5980kb
input:
6 1 3 1 2 3 1 3 5 1 4 5 1 5 6 1
output:
6
result:
ok single line: '6'
Test #5:
score: 0
Accepted
time: 1ms
memory: 5904kb
input:
6 1 2 5 3 2 3 2 4 1 4 5 2 4 6 4
output:
16
result:
ok single line: '16'
Test #6:
score: 0
Accepted
time: 0ms
memory: 5988kb
input:
6 1 6 8 2 6 2 3 6 3 4 6 5 5 6 1
output:
20
result:
ok single line: '20'
Test #7:
score: 0
Accepted
time: 1ms
memory: 5944kb
input:
7 6 4 60 4 2 463 6 7 165 6 3 81 6 1 30 4 5 214
output:
1103
result:
ok single line: '1103'
Test #8:
score: 0
Accepted
time: 1ms
memory: 6444kb
input:
7 1 3 463 1 5 214 7 2 165 7 4 81 7 1 60 7 6 30
output:
1103
result:
ok single line: '1103'
Test #9:
score: -100
Wrong Answer
time: 0ms
memory: 6080kb
input:
8 8 7 10 1 3 1 3 2 1 6 5 2 5 4 1 3 7 4 7 5 3
output:
27
result:
wrong answer 1st lines differ - expected: '24', found: '27'