QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#199360 | #6520. Classic Problem | OccDreamer | WA | 954ms | 215388kb | C++14 | 3.9kb | 2023-10-04 10:09:55 | 2023-10-04 10:09:56 |
Judging History
answer
//code by Emissary
#include<bits/stdc++.h>
#define fi first
#define se second
#define vc vector
#define db double
#define ll long long
#define mk make_pair
#define pb push_back
#define PI pair<int,int>
#define ull unsigned long long
#define err cerr << " -_- " << endl
#define debug cerr << " ------------------- " << endl
#define input(x) freopen(#x".in","r",stdin)
#define output(x) freopen(#x".out","w",stdout)
#define NO puts("No")
#define YES puts("Yes")
//#define int long long
using namespace std;
namespace IO{
inline int read(){
int X=0, W=0; char ch=getchar();
while(!isdigit(ch)) W|=ch=='-', ch=getchar();
while(isdigit(ch)) X=(X<<1)+(X<<3)+(ch^48), ch=getchar();
return W?-X:X;
}
inline void write(ll x){
if(x<0) x=-x, putchar('-');
if(x>9) write(x/10);
putchar(x%10+'0');
}
inline void sprint(ll x){write(x), putchar(32);}
inline void eprint(ll x){write(x), putchar(10);}
}using namespace IO;
const int inf = 2e9;
const int MAXN = 5e5+5;
int Pre[MAXN<<2], Nxt[MAXN<<2];
int n, m, nxt[MAXN<<2], pre[MAXN<<2];
int fa[MAXN], o[MAXN<<2], q[MAXN<<2], minn[MAXN<<2], cnt;
struct edge{
int u, v, w;
}E[MAXN<<3];
vc<PI> G[MAXN<<2];
vc<int> block, node[MAXN<<2];
set<int> S;
struct op{
int x, v; bool op;
}stk[MAXN<<2];
int topf;
inline void add(int x, int y, int w){G[x].pb(mk(y,w));}
inline void del(int x){
stk[++topf]=op{pre[x],nxt[pre[x]],0};
stk[++topf]=op{nxt[x],pre[nxt[x]],1};
nxt[pre[x]]=nxt[x];
pre[nxt[x]]=pre[x];
return ;
}
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
inline void push(int x){
o[++cnt]=x;
if(x>1) o[++cnt]=x-1;
if(x<n) o[++cnt]=x+1;
return ;
}
inline bool check(){
block.clear();
for(int i=1;i<=cnt;++i) pre[i]=i-1, nxt[i]=i+1; pre[cnt+1]=cnt; nxt[0]=1;
for(int i=1;i<=cnt;++i) if(find(i)==i) block.pb(i), minn[i]=inf;
for(int i=1;i<=cnt;++i) node[i].clear();
for(int i=1;i<=cnt;++i) node[find(i)].pb(i);
return block.size()>1;
}
inline void upd(int x, int y, int w){
if(w>minn[x]) return ;
minn[x]=w, q[x]=y;
return ;
}
inline void solve(){
n=read(), m=read(); int x, y; cnt=0;
for(int i=1;i<=m;++i) E[i].u=read(), E[i].v=read(), E[i].w=read(), push(E[i].u), push(E[i].v);
sort(o+1,o+1+cnt); cnt=unique(o+1,o+1+cnt)-o-1; ll ans=n-max(cnt,1);
for(int i=1;i<=cnt;++i) fa[i]=i, G[i].clear();
for(int i=1;i<=m;++i) E[i].u=lower_bound(o+1,o+1+cnt,E[i].u)-o, E[i].v=lower_bound(o+1,o+1+cnt,E[i].v)-o, add(E[i].u,E[i].v,E[i].w), add(E[i].v,E[i].u,E[i].w);
for(int i=1;i<=cnt;++i) sort(G[i].begin(),G[i].end());
//cerr << "cnt=" << cnt << endl;
while(cnt && check()){
//err;
for(int i=1;i<=m;++i){
x=find(E[i].u), y=find(E[i].v);
if(x==y) continue;
upd(x,y,E[i].w); upd(y,x,E[i].w);
}
for(auto i:block){
for(auto j:node[i]) Pre[j]=pre[j], del(j);
while(topf){
if(stk[topf].op==0) nxt[stk[topf].x]=stk[topf].v;
else pre[stk[topf].x]=stk[topf].v;
--topf;
}
reverse(node[i].begin(),node[i].end());
for(auto j:node[i]) Nxt[j]=nxt[j], del(j);
for(auto j:node[i]){
for(auto o:G[j]) if(o.fi==Nxt[j]) Nxt[j]=nxt[Nxt[j]];
reverse(G[j].begin(),G[j].end());
for(auto o:G[j]) if(o.fi==Pre[j]) Pre[j]=pre[Pre[j]];
reverse(G[j].begin(),G[j].end());
if(Pre[j]) upd(i,find(Pre[j]),o[j]-o[Pre[j]]);
if(Nxt[j]<=cnt) upd(i,find(Nxt[j]),o[Nxt[j]]-o[j]);
}
while(topf){
if(stk[topf].op==0) nxt[stk[topf].x]=stk[topf].v;
else pre[stk[topf].x]=stk[topf].v;
--topf;
}
}
for(auto i:block){
//cerr << i << ' ' << minn[i] << ' ' << q[i] << endl;
if(find(i)==find(q[i])) continue;
fa[find(i)]=find(q[i]); ans+=minn[i];
}
}
eprint(ans);
}
signed main(){
int t=read();
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 22ms
memory: 116212kb
input:
3 5 3 1 2 5 2 3 4 1 5 0 5 0 5 4 1 2 1000000000 1 3 1000000000 1 4 1000000000 1 5 1000000000
output:
4 4 1000000003
result:
ok 3 number(s): "4 4 1000000003"
Test #2:
score: -100
Wrong Answer
time: 954ms
memory: 215388kb
input:
16 1000000000 0 447 99681 1 2 1000000000 1 3 1000000000 2 3 1000000000 1 4 1000000000 2 4 1000000000 3 4 1000000000 1 5 1000000000 2 5 1000000000 3 5 1000000000 4 5 1000000000 1 6 1000000000 2 6 1000000000 3 6 1000000000 4 6 1000000000 5 6 1000000000 1 7 1000000000 2 7 1000000000 3 7 1000000000 4 7 ...
output:
999999999 446000000000 732256441 1154158007 -15409042874844 -13800661144205 127 4 2186 1562 1176 5100 5 503 679 4
result:
wrong answer 4th numbers differ - expected: '999999680', found: '1154158007'