QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#310507#5537. Storing EggsZIhanWA 1ms3628kbC++203.1kb2024-01-21 15:00:552024-01-21 15:00:55

Judging History

This is the latest submission verdict.

  • [2024-01-21 15:00:55]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 3628kb
  • [2024-01-21 15:00:55]
  • Submitted

answer

#include<bits/stdc++.h>
#define int long long
#define mod 1000000007
#define endl '\n'
#define pii pair<int,int>
#define I_am_weak ios::sync_with_stdio(0),cin.tie(0);
using namespace std;
int n,m;
int anc[22][100005];
int mx[22][100005];
int f[100005];
int find(int x){
    if(f[x]==x)return x;
    else return f[x]=find(f[x]);
}
struct ff{
	int x,y;
	int v;
};
bool cmp(ff a,ff b){
    return a.v<b.v;
}
vector<ff>edge,mst,last;
vector<pii>g[100005];
int in[100005],out[100005];
bool ch(int x,int y){
    return in[x]<=in[y]&&out[x]>=out[y];
}
int getlca(int x,int y){
    if(ch(x,y))return x;
    if(ch(y,x))return y;
    for(int i=21;i>=0;i--){
        if(ch(anc[i][x],y)==0){
            x=anc[i][x];
        }
    }
    return anc[0][x];
}
bool vis[100005];
int owowowowo=1;
void dfs1(int now,int pre){
    vis[now]=1;
    in[now]=++owowowowo;
    anc[0][now]=pre;
    for(auto [nxt,val]:g[now]){
        if(nxt==pre)continue;
        mx[0][nxt]=val;
        anc[0][nxt]=now;
        //cout<<now<<" "<<nxt<<" "<<val<<endl;
        dfs1(nxt,now);
    }
    out[now]=owowowowo;
}
signed main(){
	//注意多筆測資輸入!!!
	I_am_weak
	cin>>n>>m;
	for(int i=0;i<m;i++){
		ff now;
		cin>>now.x>>now.y>>now.v;
		edge.push_back(now);
	}
    for(int i=1;i<=n;i++){
        f[i]=i;
    }
    sort(edge.begin(),edge.end(),cmp);
    for(int i=0;i<m;i++){
        ff now=edge[i];
        int x=find(now.x);
        int y=find(now.y);
        if(x==y){
            last.push_back(now);
            continue;
        }else {
            mst.push_back(now);
            f[x]=y;
        }
    }
    int res=find(f[1]);
    for(int i=1;i<=n;i++){
        if(res!=find(i)){
            cout<<-1<<endl;
            return 0;
        }
    }
    sort(last.begin(),last.end(),cmp);
    for(int i=1;i<=n;i++){
        f[i]=i;
    }
    for(auto now:last){
        int x=find(now.x);
        int y=find(now.y);
        if(x==y){
            continue;
        }else {
            f[x]=y;
            g[now.x].push_back({now.y,now.v});
            g[now.y].push_back({now.x,now.v});
        }
    }
    dfs1(1,1);
    for(int i=1;i<=21;i++){
        for(int j=1;j<=n;j++){
            anc[i][j]=anc[i-1][anc[i-1][j]];
            mx[i][j]=mx[i-1][anc[i-1][j]];
        }
    }
    for(int i=1;i<=n;i++){
        if(vis[i]==0){
            cout<<-1<<endl;
            return 0;
        }
    }
    int ans=0;
    for(auto [x,y,vv]:mst){
        int lca=getlca(x,y);
        int mxx=0;
        //cout<<x<<" "<<y<<":"<<lca<<":";
        if(x!=lca){
            for(int i=21;i>=0;i--){
                if(ch(anc[i][x],lca)==0){
                    mxx=max(mxx,mx[i][x]);
                    x=anc[i][x];
                }
            }
            mxx=max(mxx,mx[0][x]);
        }
        if(y!=lca){
            for(int i=21;i>=0;i--){
                if(ch(anc[i][y],lca)==0){
                    mxx=max(mxx,mx[i][y]);
                    y=anc[i][y];
                }
            }
            
            mxx=max(mxx,mx[0][y]);
        }
        ans+=mxx-vv+1;
    }
    cout<<ans<<endl;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3628kb

input:

5 2
#....
.....
....#

output:

-1

result:

wrong answer 1st numbers differ - expected: '4.4721360', found: '-1.0000000', error = '1.2236068'