Submission #596849

# Submission time Handle Problem Language Result Execution time Memory
596849 2022-07-15T07:35:47 Z 반딧불(#8447) Reconstruction Project (JOI22_reconstruction) C++17
31 / 100
1012 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[100002];

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;
        }
    }
    exit(1);
}

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 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 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 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 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 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Runtime error 982 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 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Runtime error 1012 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 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 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 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 426 ms 28400 KB Output is correct
21 Correct 470 ms 28476 KB Output is correct
22 Correct 414 ms 28364 KB Output is correct
23 Correct 424 ms 28416 KB Output is correct
24 Correct 406 ms 28320 KB Output is correct
25 Correct 439 ms 28292 KB Output is correct
26 Correct 459 ms 28300 KB Output is correct
27 Correct 453 ms 27476 KB Output is correct
28 Correct 473 ms 29048 KB Output is correct
29 Correct 496 ms 29044 KB Output is correct
30 Correct 477 ms 28812 KB Output is correct
31 Correct 356 ms 28644 KB Output is correct
32 Correct 225 ms 21200 KB Output is correct
33 Correct 330 ms 28456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 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 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Runtime error 982 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 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 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 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Runtime error 982 ms 1048576 KB Execution killed with signal 9
21 Halted 0 ms 0 KB -