QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#116130 | #6520. Classic Problem | lxhcr | WA | 5178ms | 87512kb | C++14 | 4.7kb | 2023-06-28 10:21:00 | 2023-06-28 10:21:02 |
Judging History
你现在查看的是最新测评结果
- [2023-12-06 00:08:28]
- hack成功,自动添加数据
- (//qoj.ac/hack/481)
- [2023-08-10 23:21:45]
- System Update: QOJ starts to keep a history of the judgings of all the submissions.
- [2023-06-28 10:21:00]
- 提交
answer
#include <bits/stdc++.h>
typedef long long ll;typedef unsigned long long ull; typedef double db;typedef long double ldb;
#define fre(x) freopen(#x ".in","r",stdin),freopen(#x ".out","w",stdout)
#define Rep(i,a,b) for(int i=a;i<=b;++i)
#define Dwn(i,a,b) for(int i=a;i>=b;--i)
#define pii pair<int,int>
#define mair make_pair
#define fir first
#define sec second
using namespace std;
const int maxn=5e5+10;
int n,m;
unordered_map<int,int>MapId,UpId;
vector<pii>E[maxn];
int Id(int x){ if(MapId.count(x))return MapId[x]; MapId[x]=MapId.size()+1;E[MapId[x]].resize(0);return MapId[x]; }
vector<int>vec;
pii a[maxn],val[maxn];int tot;
ll ans;
int col[maxn];
struct UN{
int f[maxn];
void Init(int n){ Rep(i,1,n)f[i]=i; }
int Find(int x){ return f[x]==x ? x : f[x]=Find(f[x]); }
int Merge(int x,int y){
x=Find(x),y=Find(y);if(x==y)return 0;
f[y]=x;return 1;
}
}S;
void solve(){
cin>>n>>m;unordered_map<int,int>().swap(MapId);unordered_map<int,int>().swap(UpId);vec.resize(0);tot=0;ans=0;
Rep(i,1,m){
int u,v,w;cin>>u>>v>>w;E[Id(u)].emplace_back(v,w),E[Id(v)].emplace_back(u,w);
vec.emplace_back(u),vec.emplace_back(v);
}sort(vec.begin(),vec.end()),vec.erase(unique(vec.begin(),vec.end()),vec.end());
int last=0;
for(auto it : vec){
if(last+1<it)a[++tot]=pii(last+1,it-1),ans+=it-last-2;
last=it,a[++tot]=pii(it,it);
}if(last<n)a[++tot]=pii(last+1,n),ans+=n-last-1;
S.Init(tot);int Ecnt=0;Rep(i,1,tot)UpId[a[i].fir]=UpId[a[i].sec]=i;
while(Ecnt+1<tot){
Rep(i,1,tot)val[i]=pii(INT_MAX,0),col[i]=0,S.Find(i);
pii lasp[2];set<int>spe;
Rep(i,1,tot){
if(a[i].sec>a[i].fir || (!MapId.count(a[i].fir))){
if(lasp[0].sec && lasp[0].sec!=S.f[i]){
val[lasp[0].sec]=min(val[lasp[0].sec],pii(a[i].fir-lasp[0].fir,S.f[i]));
val[S.f[i]]=min(val[S.f[i]],pii(a[i].fir-lasp[0].fir,lasp[0].sec));
}else if(lasp[1].sec && lasp[1].sec!=S.f[i]){
val[lasp[1].sec]=min(val[lasp[1].sec],pii(a[i].fir-lasp[1].fir,S.f[i]));
val[S.f[i]]=min(val[S.f[i]],pii(a[i].fir-lasp[1].fir,lasp[1].sec));
}
}else{
int u=MapId[a[i].fir],x=S.f[i],y;vector<int>pp;
for(auto [v,w] : E[u]){
y=S.Find(UpId[v]);
if(x!=y){
val[x]=min(val[x],pii(w,y)); val[y]=min(val[y],pii(w,x));
if(spe.find(v)!=spe.end()){ pp.emplace_back(v);spe.erase(v); }
}
}
if(!spe.empty()){
auto it = prev(spe.end());
int t=S.Find(UpId[*it]);
if(t!=x){ val[x]=min(val[x],pii(a[i].fir-(*it),t)); val[t]=min(val[t],pii(a[i].fir-(*it),x)); }
else if(it!=spe.begin()){
--it;t=S.Find(UpId[*it]);
if(t!=x){ val[x]=min(val[x],pii(a[i].fir-(*it),t)); val[t]=min(val[t],pii(a[i].fir-(*it),x)); }
}
}
for(auto it : pp )spe.insert(it);
}
if(S.f[i]!=lasp[0].sec){
lasp[1]=lasp[0];lasp[0]=pii(a[i].sec,S.f[i]);
}else lasp[0].fir=a[i].sec;
if(col[S.f[i]])spe.erase(col[S.f[i]]);
col[S.f[i]]=a[i].sec;spe.insert(col[S.f[i]]);
}lasp[0]=lasp[1]=pii(0,0);spe.clear();Rep(i,1,tot)col[i]=0;
Dwn(i,tot,1){
if(a[i].sec>a[i].fir || (!MapId.count(a[i].fir))){
if(lasp[0].sec && lasp[0].sec!=S.f[i]){
val[lasp[0].sec]=min(val[lasp[0].sec],pii(lasp[0].fir-a[i].sec,S.f[i]));
val[S.f[i]]=min(val[S.f[i]],pii(lasp[0].fir-a[i].sec,lasp[0].sec));
}else if(lasp[1].sec && lasp[1].sec!=S.f[i]){
val[lasp[1].sec]=min(val[lasp[1].sec],pii(lasp[1].fir-a[i].sec,S.f[i]));
val[S.f[i]]=min(val[S.f[i]],pii(lasp[1].fir-a[i].sec,lasp[1].sec));
}
}else{
int u=MapId[a[i].fir],x=S.f[i],y;vector<int>pp;
for(auto [v,w] : E[u]){
y=S.Find(UpId[v]);
if(x!=y){
val[x]=min(val[x],pii(w,y)); val[y]=min(val[y],pii(w,x));
if(spe.find(v)!=spe.end()){ pp.emplace_back(v);spe.erase(v); }
}
}
if(!spe.empty()){
auto it = spe.begin();
int t=S.Find(UpId[*it]);
if(t!=x){ val[x]=min(val[x],pii((*it)-a[i].fir,t)); val[t]=min(val[t],pii((*it)-a[i].fir,x)); }
else if(next(it)!=spe.end()){
++it;t=S.Find(UpId[*it]);
if(t!=x){ val[x]=min(val[x],pii((*it)-a[i].fir,t)); val[t]=min(val[t],pii((*it)-a[i].fir,x)); }
}
}
for(auto it : pp )spe.insert(it);
}
if(S.f[i]!=lasp[0].sec){
lasp[1]=lasp[0];lasp[0]=pii(a[i].fir,S.f[i]);
}else lasp[0].fir=a[i].fir;
if(col[S.f[i]])spe.erase(col[S.f[i]]);
col[S.f[i]]=a[i].fir;spe.insert(col[S.f[i]]);
}
Rep(i,1,tot)if(S.Find(i)==i && val[i].fir!=INT_MAX){
if(S.Merge(i,val[i].sec)){ ++Ecnt;ans+=val[i].fir; }
}
}
cout<<ans<<"\n";
}
int tex;
int main (){ ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);cin>>tex;while(tex--)solve(); }
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 20772kb
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: 5178ms
memory: 87512kb
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 762066189 999999680 999899999 999966830 127 4 2186 1562 1176 5100 5 503 679 4
result:
wrong answer 3rd numbers differ - expected: '732256441', found: '762066189'