诶秒出正解的题目写起来就是爽
有一个结论:在生成树上添加一条新边E{a,b,v}使得新图的最小生成树改变,充要条件是E和生成树形成的环中,v不是权值最大的边。
对最小生成树进行树链剖分,按树链剖分序建线段树
对于第n~m条边E{a,b,v},将a,b路径上的最长边求出来e{a0,b0,v0},v-v0即为该边能减小最多的值,增加最多的值为INF
对于前1~n-1条边E{a,b,v},能增加最多的值即为使E在第i条边与生成树连接的环中,第i条边权值的最小值-v,减少最多的值为INF
本机出了数据平均要跑5s+,UVALive评测及2.8秒就飞过去了
诶,别人的评测机真牛逼
Notice:1、注意最后答案取值最大超过1e17,答案要开long long
2、输出的case冒号后面有个空格,否则PE
(诶为了1A,写了造数据和暴力对拍)
正解:
#include
#include
#include
#include
#define N 40050
#define M 400050
#define INF (1<<30)
#define mid ((l+r)>>1)
#define ls l,mid,(t<<1)
#define rs mid+1,r,(t<<1^1)
#define L (t<<1)
#define R (t<<1^1)
using namespace std;
typedef long long LL;
int tr[4*N],ag[4*N],S,P;
int head[N],cnt,tot;
int son[N],D[N],siz[N],w[N],top[N],A[N],fa[N];
int vson[N];
int a[M],b[M],v[M],n,m;
inline int read() {
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
vector E[N],Q[N];
struct Edge{ int a,b,v,next; }e[M];
void add_Edge(int a,int b,int v) {
e[++cnt].a = a;
e[cnt].b = b;
e[cnt].v = v;
e[cnt].next = head[a];
head[a] = cnt;
}
void dfs1(int u,int f) {
D[u] = D[f] + 1; siz[u] = 1; fa[u] = f;
for (int i=0;i siz[ son[u] ]) son[u] = v , vson[u] = Q[u][i];
}
}
void dfs2(int u,int h,int q) {
top[u] = h; w[u] = ++tot; A[tot] = q;
if (son[u]) dfs2(son[u],h,vson[u]);
for (int i=0;i= ll && r <= rr) { ag[t] = min(ag[t],V); return ; }
push_down(t);
if (ll <= mid) update(ls);
if (rr > mid) update(rs);
}
void query(int l,int r,int t) {
if (l >= ll && r <= rr) {
S = max(tr[t],S); P = min(ag[t],P);
return ;
}
push_down(t);
if (ll <= mid) query(ls);
if (rr > mid) query(rs);
tr[t] = max(tr[L],tr[R]);
}
void jump(int &x,int &y) {
if (top[x] == top[y]) {
if (D[x] > D[y]) swap(x,y);
ll = w[ son[x] ]; rr = w[y]; y = x;
} else {
if (D[ top[x] ] < D[ top[y] ]) swap(x,y);
ll = w[ top[x] ]; rr = w[x];
x = fa[ top[x] ];
}
}
void solve(int cas) {
long long ans = 0LL;
n = read(); m = read();
for (int i=1;i<=m;i++) {
a[i] = read(); b[i] = read(); v[i] = read();
add_Edge(a[i],b[i],v[i]);
add_Edge(b[i],a[i],v[i]);
if (i < n) {
E[ a[i] ].push_back( b[i] );
Q[ a[i] ].push_back( v[i] );
E[ b[i] ].push_back( a[i] );
Q[ b[i] ].push_back( v[i] );
}
}
dfs1(1,1); dfs2(1,0,0);
build(1,n,1);
for (int i=n;i<=m;i++) {
int x,y;int p = INF;
x = a[i] ; y = b[i]; V = v[i];
while (x != y) {
S = 0;
jump(x,y); query(1,n,1);
p = min(p,V - S);
}
x = a[i] ; y = b[i]; V = v[i];
while (x != y)
jump(x,y) , update(1,n,1);
ans += 1LL*-i + 1LL*i*i*p;
}
for (int i=1;i<=n-1;i++) {
int x = a[i] , y = b[i]; jump(x,y); P = INF;
query(1,n,1);
if (P == INF) ans += -1LL*i - 1LL*i*i;
else ans += 1LL*i*(P-v[i]) - 1LL*i*i;
}
printf("Case %d: ",cas);
printf("%lld\n",ans);
for (int i=1;i<=n;i++) E[i].clear() , Q[i].clear();
}
void init() {
cnt = tot = 0;
memset(e,0,sizeof(e));
memset(head,0,sizeof(head));
memset(son,0,sizeof(son));
memset(D,0,sizeof(D));
//memset(ag,127/3,sizeof(ag));
/*
memset(tr,0,sizeof(tr));
memset(vson,0,sizeof(vson));
memset(A,0,sizeof(A));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(v,0,sizeof(v));
memset(siz,0,sizeof(siz));
memset(w,0,sizeof(w));
memset(top,0,sizeof(top));
memset(fa,0,sizeof(fa));
*/
}
int main() {
int T = read();
for (int i=1;i<=T;i++) {
init();
solve(i);
}
return 0;
}
#include
#include
#include
#include
#include
#include
#define N 1000050
using namespace std;
const int maxn = 10000;
const int maxm = 100000;
const int V = 1001;
const int cas = 25;
struct Edge{ int a,b,v,p; }e[N];
int fa[N],n,m,cnt;
bool cmp(Edge p1,Edge p2) { return p1.v < p2.v; }
int get(int x) { return fa[x] == x ? x : fa[x] = get(fa[x]); }
int main()
{
freopen("problem6.in","w",stdout);
srand(time(0));
int T = rand() % cas + 1;
T = 25;
printf("%d\n",T);
while (T--) {
memset(e,0,sizeof(e)); cnt = 0;
n = rand() * rand() % maxn + 2; m = rand() * rand() % maxm + n;
printf("%d %d\n",n,m);
for (int i=1;i<=n-1;i++) {
e[++cnt].a = i+1;
e[cnt].b = rand() * rand() % i + 1;
e[cnt].v = rand() * rand() % V + 1;
}
for (int i=n;i<=m;i++) {
e[++cnt].a = rand() * rand() % (n-1) + 2;
e[cnt].b = rand() * rand() % (e[cnt].a-1) + 1;
e[cnt].v = rand() % V ;
}
sort(e+1,e+cnt+1,cmp);
for (int i=1;i<=n;i++) fa[i] = i;
for (int i=1;i<=cnt;i++)
if (get( e[i].a ) != get( e[i].b )) {
fa[ get(e[i].a) ] = get( e[i].b );
e[i].p = 1;
}
for (int i=1;i<=cnt;i++) if ( e[i].p)
printf("%d %d %d\n",e[i].a,e[i].b,e[i].v);
for (int i=1;i<=cnt;i++) if (!e[i].p)
printf("%d %d %d\n",e[i].a,e[i].b,e[i].v);
}
fclose(stdout);
return 0;
}
#include
#include
#include
#include
#define N 1000050
#define INF 1001
using namespace std;
typedef long long LL;
struct Edge{ int a,b,v,p;}e[N];
int d[N],fa[N],n,m;
bool cmp(int p1,int p2) {
return e[p1].v == e[p2].v ? e[p1].p > e[p2].p : e[p1].v < e[p2].v;
}
int get(int x) { return fa[x] == x ? x : fa[x] = get( fa[x] ); }
LL go(int x) {
int tmp = e[x].v , p1 = INF , p2 = -1;
for (int j=-1;j<=INF;j++) {
e[x].v = j;
for (int i=1;i<=m;i++) d[i] = i;
for (int i=1;i<=n;i++) fa[i] = i;
sort(d+1,d+m+1,cmp);
bool flag = true;
for (int i=1;i<=m;i++)
if ( get( e[ d[i] ].a ) != get( e[ d[i] ].b ) ) {
if (!e[ d[i] ].p) flag = false;
fa[ get(e[ d[i] ].a ) ] = get( e[ d[i] ].b );
}
if (flag) p1 = min(p1,j) , p2 = max(p2,j);
e[x].v = tmp;
}
LL r = 0;
if (p1 == -1) r += -1LL*x*x; else r += 1LL*x*x*(tmp-p1);
if (p2 == INF) r += -1LL*x; else r += 1LL*x*(p2-tmp);
return r;
//return 1LL*x*(p2-tmp) + 1LL*x*x*(tmp-p1);
}
void solve() {
memset(e,0,sizeof(e));
LL ans = 0LL;
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++) {
scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].v);
if (i <= n-1) e[i].p = 1;
}
for (int i=1;i<=m;i++) ans += go(i);
cout << ans << endl;
}
int main()
{
freopen("problem6.in","r",stdin);
freopen("problem6_cmp.out","w",stdout);
int T = 0; scanf("%d",&T);
for (int i=1;i<=T;i++) {
printf("Case %d: ",i); solve();
}
fclose(stdin); fclose(stdout);
return 0;
}