Submission #596842

# Submission time Handle Problem Language Result Execution time Memory
596842 2022-07-15T07:27:31 Z 반딧불(#8447) Reconstruction Project (JOI22_reconstruction) C++17
3 / 100
1103 ms 1048576 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct dat{
    int v, w, idx;
    dat(){}
    dat(int v, int w, int idx): v(v), w(w), idx(idx){}
};

struct Inversion{
    int x, y; ll t;
    Inversion(){}
    Inversion(int x, int y, ll t): x(x), y(y), t(t){}
    bool operator<(const Inversion &r)const{
        if(t!=r.t) return t<r.t;
        if(x==y || r.x==r.y) return x==y;
        return x<r.x;
    }
};

struct unionFind{
    int par[1002];

    void init(int _n){
        for(int i=1; i<=_n; i++) par[i] = i;
    }

    int find(int x){
        if(x == par[x]) return x;
        return par[x] = find(par[x]);
    }

    void merge(int x, int y){
        x = find(x), y = find(y);
        par[x] = y;
    }
} dsu;

struct Edge{
    int x, y; ll w;
    int idx;
    Edge(){}
    Edge(int x, int y, ll w): x(x), y(y), w(w){}
    bool operator<(const Edge &r)const{
        return w<r.w;
    }
};

void input();
void operate();

int main(){
    input();
    operate();
}

int n, m, q;
Edge arr[100002];
ll query[1000002];

void input(){
    scanf("%d %d", &n, &m);
    for(int i=1; i<=m; i++){
        scanf("%d %d %lld", &arr[i].x, &arr[i].y, &arr[i].w);
    }
    sort(arr+1, arr+m+1);
    for(int i=1; i<=m; i++) arr[i].idx = i;

    scanf("%d", &q);
    for(int i=1; i<=q; i++){
        scanf("%lld", &query[i]);
    }
}

vector<Inversion> edgeInversion;

void calculateEdgeInversion(){
    for(int i=1; i<=m; i++){
        for(int j=i+1; j<=m; j++){
            if(arr[i].w == arr[j].w) continue;
            int a = arr[i].w>arr[j].w ? j : i;
            edgeInversion.push_back(Inversion(a, i+j-a, (arr[i].w+arr[j].w+1)/2));
        }
        edgeInversion.push_back(Inversion(i, i, arr[i].w));
    }
    sort(edgeInversion.begin(), edgeInversion.end());
}

vector<dat> link[502];
ll currentTime;
ll ta, tb;
bool isUsed[1002];

void makeInitialGraph(){
    vector<Edge> vec (arr+1, arr+m+1);
    sort(vec.begin(), vec.end());
    dsu.init(n);
    for(Edge edge: vec){
        if(dsu.find(edge.x) == dsu.find(edge.y)) continue;
        dsu.merge(edge.x, edge.y);
        ta-=1, tb+=edge.w;
        link[edge.x].push_back(dat(edge.y, edge.w, edge.idx));
        link[edge.y].push_back(dat(edge.x, edge.w, edge.idx));
        isUsed[edge.idx] = 1;
    }
}

int findEdge(int x, int p, int goal, int goalEdge, bool found=0){
    if(goal == x) return found ? goalEdge : 0;
    for(dat y: link[x]){
        if(y.v == p) continue;
        int tmp = findEdge(y.v, x, goal, goalEdge, found || y.idx==goalEdge);
        if(tmp) return tmp;
    }
    return 0;
}

void eraseLink(int x, int idx){
    for(auto it = link[x].begin(); it != link[x].end(); ++it){
        if(it->idx == idx){
            link[x].erase(it);
            return;
        }
    }
    x = idx;
    while((x-idx)%2==0) x+=2;
}

void processQueries(){
    int pnt = 0;
    for(int i=1; i<=q; i++){
        while(pnt < (int)edgeInversion.size() && edgeInversion[pnt].t <= query[i]){
            Inversion tmp = edgeInversion[pnt];
            if(tmp.x == tmp.y){ /// �ּ����� ����
                if(isUsed[tmp.y]) ta += 2, tb -= tmp.t*2;
            }
            else{ /// �������� ����
                if(isUsed[tmp.y] || !isUsed[tmp.x]){
                    pnt++;
                    continue;
                }
                Edge edge = arr[tmp.y];
                currentTime = tmp.t;
                int tmin = findEdge(edge.x, -1, edge.y, tmp.x);
                if(tmin == tmp.x){
                    eraseLink(arr[tmin].x, tmin);
                    eraseLink(arr[tmin].y, tmin);
                    ta -= (currentTime >= arr[tmin].w ? 1 : -1);
                    tb -= arr[tmin].w * (currentTime >= arr[tmin].w ? -1 : 1);
                    isUsed[tmin] = 0;

                    link[edge.x].push_back(dat(edge.y, edge.w, edge.idx));
                    link[edge.y].push_back(dat(edge.x, edge.w, edge.idx));
                    ta += (currentTime >= edge.w ? 1 : -1);
                    tb += edge.w * (currentTime >= edge.w ? -1 : 1);
                    isUsed[edge.idx] = 1;
                }
            }
            pnt++;
        }

        currentTime = query[i];
        printf("%lld\n", ta * currentTime + tb);
    }
}

void operate(){
    calculateEdgeInversion();
    makeInitialGraph();
    processQueries();
}

Compilation message

reconstruction.cpp: In function 'void input()':
reconstruction.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
reconstruction.cpp:67:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         scanf("%d %d %lld", &arr[i].x, &arr[i].y, &arr[i].w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:72:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
reconstruction.cpp:74:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |         scanf("%lld", &query[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Runtime error 1032 ms 1048576 KB Execution killed with signal 9
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Runtime error 1103 ms 1048576 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 360 KB Output is correct
20 Correct 423 ms 29712 KB Output is correct
21 Correct 440 ms 28952 KB Output is correct
22 Correct 439 ms 28940 KB Output is correct
23 Correct 434 ms 28888 KB Output is correct
24 Correct 420 ms 28888 KB Output is correct
25 Correct 427 ms 28884 KB Output is correct
26 Correct 432 ms 28924 KB Output is correct
27 Runtime error 130 ms 31376 KB Execution killed with signal 11
28 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Runtime error 1032 ms 1048576 KB Execution killed with signal 9
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Runtime error 1032 ms 1048576 KB Execution killed with signal 9
21 Halted 0 ms 0 KB -