답안 #640112

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
640112 2022-09-13T16:43:34 Z victor_gao Reconstruction Project (JOI22_reconstruction) C++17
7 / 100
2135 ms 1048576 KB
//#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define pii pair<int,int>
#define x first
#define y second
#define N 515
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
struct dsu{
    int boss[N]={},sz[N]={};
    void init(int n){
        for (int i=1;i<=n;i++){
            boss[i]=i;
            sz[i]=1;
        }
    }
    int find(int x){
        if (boss[x]==x) return x;
        return boss[x]=find(boss[x]);
    }
    void Merge(int a,int b){
        int ra=find(a);
        int rb=find(b);
        if (ra==rb) return;
        if (sz[ra]>sz[rb]){
            boss[rb]=ra;
            sz[ra]+=sz[rb];
        }
        else {
            boss[ra]=rb;
            sz[rb]+=ra;
        }
    }
}d;
int n,m,q;
int fa[N],use_edge[N],dep[N];
long long ans[1000015];
bool vis[100015];
vector<pii>qus;
vector<pair<int,pii> >change;
vector<pair<int,pii> >edge;
set<pii>g[N];
vector<pii>vt;
int count(int pos,int c){
    return abs(edge[pos].x-c);
}
void dfs(int p,int lp,int use){
    fa[p]=lp;
    use_edge[p]=use;
    dep[p]=dep[lp]+1;
    for (auto i:g[p]){
        if (i.x!=lp) dfs(i.x,p,i.y);
    }
}
pii find_min(int a,int b,int val){
    int mx_val=0,mx_edge=0,mx_pos=0;
    while (a!=b){
        if (dep[a]>dep[b]){
            if (mx_val<count(use_edge[a],val)&&a!=1){
                mx_val=count(use_edge[a],val);
                mx_edge=use_edge[a];
                mx_pos=a;
            }
            a=fa[a];
        }
        else {
            if (mx_val<count(use_edge[b],val)&&b!=1){
                mx_val=count(use_edge[b],val);
                mx_edge=use_edge[b];
                mx_pos=b;
            }
            b=fa[b];
        }
    }
    return {mx_edge,mx_pos};
}
void Remove(int num){
    g[edge[num].y.x].erase({edge[num].y.y,num});
    g[edge[num].y.y].erase({edge[num].y.x,num});
}
void In(int num){
    g[edge[num].y.x].insert({edge[num].y.y,num});
    g[edge[num].y.y].insert({edge[num].y.x,num});
}
void modify(int num,int out){
    int mid=(edge[num].x+edge[out].x+1)/2;
    change.push_back({mid,{out,num}});
    Remove(out);
    In(num);
    dfs(1,0,-1);
}
signed main(){
    fast
    cin>>n>>m;
    d.init(n);
    for (int i=1;i<=m;i++){
        int a,b,w; cin>>a>>b>>w;
        edge.push_back({w,{a,b}});
    }
    sort(edge.begin(),edge.end());
    for (int di=0;di<m;di++){
        pair<int,pii>i=edge[di];
        if (d.find(i.y.x)!=d.find(i.y.y)){
            d.Merge(i.y.x,i.y.y);
            g[i.y.x].insert({i.y.y,di});
            g[i.y.y].insert({i.y.x,di});
            vis[di]=1;
        }
    }
    dfs(1,0,-1);
    /*
    cout<<'\n';
    for (auto i:edge)
        cout<<i.y.x<<" "<<i.y.y<<' '<<i.x<<'\n';
    cout<<'\n';
    cout<<"father: ";
    for (int i=1;i<=n;i++){
        cout<<fa[i]<<' ';
    }
    cout<<'\n';
    cout<<"Dep: ";
    for (int i=1;i<=n;i++){
        cout<<dep[i]<<' ';
    }
    cout<<'\n';
    */
    for (int i=0;i<m;i++){
        if (vis[i]){
            vt.push_back({edge[i].x,i});
            continue;
        } 
        pii now_change=find_min(edge[i].y.x,edge[i].y.y,edge[i].x);
       // cout<<"Edge "<<edge[i].y.x<<" "<<edge[i].y.y<<" "<<edge[i].x<<" find "<<now_change.x<<" , "<<now_change.y<<'\n';
        modify(i,now_change.x);
    }
    sort(change.begin(),change.end());
    cin>>q;
    for (int i=1;i<=q;i++){
        int np; cin>>np;
        qus.push_back({np,i});
    }
    sort(qus.begin(),qus.end());
    sort(vt.begin(),vt.end());
    long long p=0,tans=0,val_p=0,big=0,small=0;
    for (auto i:vt)
        big+=i.x;
    for (auto i:qus){
        while (p<change.size()&&i.x>=change[p].x){
            vector<pii>now;
            bool in=0;
            pii ch=change[p].y;
            for (auto j:vt){
                if (j.y==ch.x) continue;
                else now.push_back(j);
            }
            now.push_back({edge[ch.y].x,ch.y});
            sort(now.begin(),now.end());
            swap(now,vt);
            val_p=0; small=0; big=0;
            for (auto j:vt)
                big+=j.x;
            p++;
        }
        while (val_p<vt.size()&&vt[val_p].x<i.x){
            big-=vt[val_p].x;
            small+=vt[val_p].x;
            val_p++;
        }
        /*
        cout<<"qus: "<<i.x<<" ->\n";
        for (auto j:vt)
            cout<<j.x<<" ";
        cout<<'\n';
        cout<<val_p<<' '<<big<<' '<<small<<'\n';
        */
        ans[i.y]=big-small+(long long)(val_p-(n-1-val_p))*i.x;
    }
    for (int i=1;i<=q;i++)
        cout<<ans[i]<<'\n';
}

Compilation message

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:155:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  155 |         while (p<change.size()&&i.x>=change[p].x){
      |                ~^~~~~~~~~~~~~~
reconstruction.cpp:157:18: warning: unused variable 'in' [-Wunused-variable]
  157 |             bool in=0;
      |                  ^~
reconstruction.cpp:171:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  171 |         while (val_p<vt.size()&&vt[val_p].x<i.x){
      |                ~~~~~^~~~~~~~~~
reconstruction.cpp:151:19: warning: unused variable 'tans' [-Wunused-variable]
  151 |     long long p=0,tans=0,val_p=0,big=0,small=0;
      |                   ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 360 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Runtime error 490 ms 1048576 KB Execution killed with signal 9
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 360 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Runtime error 490 ms 1048576 KB Execution killed with signal 9
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1900 ms 30656 KB Output is correct
5 Correct 1917 ms 30968 KB Output is correct
6 Correct 1882 ms 30828 KB Output is correct
7 Correct 2101 ms 32628 KB Output is correct
8 Correct 2135 ms 32660 KB Output is correct
9 Correct 2083 ms 33028 KB Output is correct
10 Correct 1426 ms 30756 KB Output is correct
11 Correct 1714 ms 32832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 360 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Runtime error 490 ms 1048576 KB Execution killed with signal 9
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 360 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Runtime error 490 ms 1048576 KB Execution killed with signal 9
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 360 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Runtime error 490 ms 1048576 KB Execution killed with signal 9
11 Halted 0 ms 0 KB -