Iowa_Battleship

BZOJ1060或洛谷1131 [ZJOI2007]时态同步

洛谷原题链接

#include<cstdio>
using namespace std;
typedef long long ll;
const int N = 5e5 + 10;
const int M = N << 1;
int fi[N], da[M], di[M], ne[M], l, ro;
ll f[N], s;
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar())
p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
return p ? -x : x;
}
inline ll maxn(ll x, ll y) { return x > y ? x : y; }
inline void add(int x, int y, int z)
{
di[++l] = y; da[l] = z; ne[l] = fi[x]; fi[x] = l;
di[++l] = x; da[l] = z; ne[l] = fi[y]; fi[y] = l;
}
void dfs_1(int x, int fa)
{
int i, y; ll ma = f[x];
for (i = fi[x]; i; i = ne[i])
if ((y = di[i]) ^ fa)
{
f[y] = da[i] + f[x];
dfs_1(y, x);
ma = maxn(ma, f[y]);
}
f[x] = ma;
}
void dfs_2(int x, int fa, ll k)
{
int i, y;
s += f[ro] - f[x]; k += f[ro] - f[x];
for (i = fi[x]; i; i = ne[i])
if ((y = di[i]) ^ fa)
f[y] += k, dfs_2(y, x, k);
}
int main()
{
int i, n, x, y, z;
n = re(); ro = re();
for (i = 1; i < n; i++)
{
x = re(); y = re(); z = re();