답안 #622461

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
622461 2022-08-04T09:56:28 Z PoonYaPat City Mapping (NOI18_citymapping) C++14
41 / 100
6 ms 8360 KB
#include "citymapping.h"

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,int> pii;
typedef tuple<int,int,int> t;

int n;
vector<int> v[1001];
vector<t> ans;
ll dis[1001][1001];

void find(int x) { //have v[x] represent the node which has x as it child, know dis[x][s]
    int r;
    ll longest=0;
    vector<pii> temp; //dis and node in backbone
    vector<int> tree;

    for (auto s : v[x]) if (dis[x][s]>longest) longest=dis[x][s], r=s; //find backbone

    for (auto s : v[x]) {
        dis[s][r]=dis[r][s]=get_distance(s,r);

        if (dis[x][s]+dis[s][r]==dis[x][r]) temp.push_back(pii(dis[x][s],s)); //in backbone
        else tree.push_back(s);
    }

    sort(temp.begin(),temp.end());
    ans.push_back(t(x,temp[0].second,temp[0].first));

    for (int i=1; i<temp.size(); ++i) {
        ans.push_back(t(temp[i-1].second,temp[i].second,temp[i].first-temp[i-1].first));
    }

    for (auto s : tree) {
        int t=dis[x][s]-(dis[x][s]+dis[s][r]-dis[x][r])/2;
        for (auto b : temp) {
            if (t==b.first) {
                v[b.second].push_back(s);
                dis[b.second][s]=dis[s][b.second]=(dis[x][s]+dis[s][r]-dis[x][r])/2;
                break;
            }
        }
    }

    for (auto s : temp) if (v[s.second].size()) find(s.second);
}

void find_roads(int N, int Q, int A[], int B[], int W[]) {
    n=N;

    //find rim
    int l=1,r=2;
    for (int x=3; x<=n; ++x) {
        dis[x][l]=dis[l][x]=get_distance(x,l);
        dis[x][r]=dis[r][x]=get_distance(r,x);
        if (!dis[l][r]) dis[l][r]=dis[r][l]=get_distance(l,r);

        if (dis[x][l]+dis[l][r]==dis[x][r]) l=x;
    }

    bool rim=true;
    ll low=LLONG_MAX;
    int nn;
    for (int i=1; i<=n; ++i) if (i!=l) {
        dis[i][l]=dis[l][i]=get_distance(l,i);
        if (low>dis[i][l]) {
            low=dis[i][l];
            nn=i;
        }
    }

    for (int i=1; i<=n; ++i) if (i!=l) {
        if (get_distance(l,nn)+get_distance(nn,i)!=dis[l][i]) rim=false;
    }

    assert(rim);

    for (int i=1; i<=n; ++i) {
        if (i==l) continue;
        v[l].push_back(i);
        if (!dis[l][i]) dis[l][i]=dis[i][l]=get_distance(i,l);
    }
    find(l);
//cout<<"\n";
    for (int i=0; i<ans.size(); ++i) tie(A[i],B[i],W[i])=ans[i];
	return;
}
/*
5 500000 1
4 5 1
1 2 1
2 3 1
3 4 1

10 500000 2
1 4 678352431
4 2 867263542
3 4 986735463
5 3 564837284
6 2 103434343
6 7 127684574
8 1 765787345
1 9 578493867
2 10 678587475

10 500000 2
1 4 8
4 2 1
3 4 7
5 3 3
6 2 10
6 7 12
8 1 7
1 9 12
2 10 3
*/

Compilation message

citymapping.cpp: In function 'void find(int)':
citymapping.cpp:32:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for (int i=1; i<temp.size(); ++i) {
      |                   ~^~~~~~~~~~~~
citymapping.cpp: In function 'void find_roads(int, int, int*, int*, int*)':
citymapping.cpp:87:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |     for (int i=0; i<ans.size(); ++i) tie(A[i],B[i],W[i])=ans[i];
      |                   ~^~~~~~~~~~~
citymapping.cpp: In function 'void find(int)':
citymapping.cpp:37:54: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
   37 |         int t=dis[x][s]-(dis[x][s]+dis[s][r]-dis[x][r])/2;
      |                                              ~~~~~~~~^
citymapping.cpp: In function 'void find_roads(int, int, int*, int*, int*)':
citymapping.cpp:75:44: warning: 'nn' may be used uninitialized in this function [-Wmaybe-uninitialized]
   75 |         if (get_distance(l,nn)+get_distance(nn,i)!=dis[l][i]) rim=false;
      |                                ~~~~~~~~~~~~^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8276 KB Correct: 5987 out of 500000 queries used.
2 Correct 4 ms 7252 KB Correct: 6606 out of 500000 queries used.
3 Correct 5 ms 8276 KB Correct: 8508 out of 500000 queries used.
4 Correct 5 ms 8276 KB Correct: 9414 out of 500000 queries used.
5 Correct 5 ms 8360 KB Correct: 10205 out of 500000 queries used.
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8276 KB Correct: 5987 out of 500000 queries used.
2 Correct 4 ms 7252 KB Correct: 6606 out of 500000 queries used.
3 Correct 5 ms 8276 KB Correct: 8508 out of 500000 queries used.
4 Correct 5 ms 8276 KB Correct: 9414 out of 500000 queries used.
5 Correct 5 ms 8360 KB Correct: 10205 out of 500000 queries used.
6 Correct 4 ms 7892 KB Correct: 5969 out of 500000 queries used.
7 Correct 4 ms 7380 KB Correct: 6953 out of 500000 queries used.
8 Incorrect 3 ms 4948 KB Reported list of edges differ from actual.
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7764 KB Correct: 5939 out of 12000 queries used.
2 Correct 4 ms 6408 KB Correct: 5951 out of 12000 queries used.
3 Correct 6 ms 7636 KB Correct: 5993 out of 12000 queries used.
4 Correct 5 ms 7100 KB Correct: 5951 out of 12000 queries used.
5 Correct 6 ms 8256 KB Correct: 5939 out of 12000 queries used.
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7764 KB Correct: 5939 out of 12000 queries used.
2 Correct 4 ms 6408 KB Correct: 5951 out of 12000 queries used.
3 Correct 6 ms 7636 KB Correct: 5993 out of 12000 queries used.
4 Correct 5 ms 7100 KB Correct: 5951 out of 12000 queries used.
5 Correct 6 ms 8256 KB Correct: 5939 out of 12000 queries used.
6 Correct 5 ms 8276 KB Correct: 5981 out of 12000 queries used.
7 Correct 4 ms 7596 KB Correct: 5969 out of 12000 queries used.
8 Correct 4 ms 7508 KB Correct: 5993 out of 12000 queries used.
9 Correct 4 ms 7892 KB Correct: 5975 out of 12000 queries used.
10 Correct 4 ms 7508 KB Correct: 5957 out of 12000 queries used.
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8276 KB Correct: 5987 out of 500000 queries used.
2 Correct 4 ms 7252 KB Correct: 6606 out of 500000 queries used.
3 Correct 5 ms 8276 KB Correct: 8508 out of 500000 queries used.
4 Correct 5 ms 8276 KB Correct: 9414 out of 500000 queries used.
5 Correct 5 ms 8360 KB Correct: 10205 out of 500000 queries used.
6 Correct 4 ms 7892 KB Correct: 5969 out of 500000 queries used.
7 Correct 4 ms 7380 KB Correct: 6953 out of 500000 queries used.
8 Incorrect 3 ms 4948 KB Reported list of edges differ from actual.
9 Halted 0 ms 0 KB -