Submission #39544

# Submission time Handle Problem Language Result Execution time Memory
39544 2018-01-16T11:15:20 Z smu201111192 Ideal city (IOI12_city) C++14
55 / 100
1000 ms 20488 KB
#include <iostream>
#include <cstdio>
#include <fstream>
#include <cassert>
#include <string>
#include <sstream>
#include <algorithm>
#include <set>
#include <numeric>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <queue>
#include <numeric>
#include <iomanip>
#define ll long long
using namespace std;
const int MAXN = 100005;
map<pair<int,int>,int> mp;
const int dy[4]={0,0,1,-1};
const int dx[4]={1,-1,0,0};
vector<int> adj[MAXN];
const int mod = 1000000000;
long long dp[MAXN];
long long ts[MAXN];
long long parent[MAXN],sz[MAXN];
int vis[MAXN];
void init(){
    memset(vis,0,sizeof(vis));
    for(int i=0;i<MAXN;i++) adj[i].clear();
    for(int i=0;i<MAXN;i++) parent[i] = i,sz[i] = 1 , dp[i] = 0;
}
int find(int u){
    return u == parent[u] ? u : parent[u] = find(parent[u]);
}
void mrg(int u,int v){
    u = find(u), v = find(v);
    if(u == v)return;
    parent[u] = v;
    sz[v] += sz[u];
}
void dfs(int here){
    vis[here] = 1;
    ts[here] = sz[find(here)];
    for(int i=0;i<adj[here].size();i++){
        int there = adj[here][i];
        if(vis[there]) continue;
        dfs(there);
        ts[here] += ts[there];
    }
}
void dfs2(int here,long long &ret,long long N){
    vis[here] = 1;
    ret %= mod;
    for(int i=0;i<adj[here].size();i++){
        int there = adj[here][i];
        if(vis[there]) continue;
        ret += ts[there] * (N-ts[there]);
        dfs2(there,ret,N);
    }
}
void addEdge(int u,int v){
    adj[find(u)].push_back(find(v));
    adj[find(v)].push_back(find(u));
}
int DistanceSum(int N, int *X, int *Y) {
    mp.clear();
    for(int i=0;i<N;i++){
        swap(X[i],Y[i]);
    }
    for(int i=0;i<N;i++){
        mp[make_pair(Y[i],X[i])] = -1;
    }
    int id = 0;
    for(auto it = mp.begin(); it!= mp.end(); it++){
        it->second = ++id;
    }
    init();
    for(int i=0;i<N;i++){
        pair<int,int> cur = make_pair(Y[i],X[i]);
        pair<int,int> bef = make_pair(Y[i]-1,X[i]);
        pair<int,int> nxt = make_pair(Y[i]+1,X[i]);
        if(mp.find(bef) != mp.end()) mrg(mp[bef],mp[cur]);
        if(mp.find(nxt) != mp.end()) mrg(mp[nxt],mp[cur]);
    }
    for(int i=0;i<N;i++){
        pair<int,int> cur = make_pair(Y[i],X[i]);
        pair<int,int> bef = make_pair(Y[i],X[i]-1);
        pair<int,int> nxt = make_pair(Y[i],X[i]+1);
        if(mp.find(bef) != mp.end()) addEdge(mp[bef],mp[cur]);
        if(mp.find(nxt) != mp.end()) addEdge(mp[nxt],mp[cur]);
    }
    long long ret = 0;
    dfs(find(1));
    memset(vis,0,sizeof(vis));
    dfs2(find(1),ret,N);
    
    init();
    for(int i=0;i<N;i++){
        pair<int,int> cur = make_pair(Y[i],X[i]);
        pair<int,int> bef = make_pair(Y[i],X[i]-1);
        pair<int,int> nxt = make_pair(Y[i],X[i]+1);
        if(mp.find(bef) != mp.end()) mrg(mp[bef],mp[cur]);
        if(mp.find(nxt) != mp.end()) mrg(mp[nxt],mp[cur]);
    }
    for(int i=0;i<N;i++){
        pair<int,int> cur = make_pair(Y[i],X[i]);
        pair<int,int> bef = make_pair(Y[i]-1,X[i]);
        pair<int,int> nxt = make_pair(Y[i]+1,X[i]);
        if(mp.find(bef) != mp.end()) addEdge(mp[bef],mp[cur]);
        if(mp.find(nxt) != mp.end()) addEdge(mp[nxt],mp[cur]);
    }
    dfs(find(1));
    memset(vis,0,sizeof(vis));
    dfs2(find(1),ret,N);
    
    return (int)ret;
}

Compilation message

city.cpp: In function 'void dfs(int)':
city.cpp:48:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<adj[here].size();i++){
                  ^
city.cpp: In function 'void dfs2(int, long long int&, long long int)':
city.cpp:58:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<adj[here].size();i++){
                  ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 8020 KB Output is correct
2 Correct 3 ms 8020 KB Output is correct
3 Correct 0 ms 8020 KB Output is correct
4 Correct 0 ms 8020 KB Output is correct
5 Correct 0 ms 8020 KB Output is correct
6 Correct 0 ms 8020 KB Output is correct
7 Correct 3 ms 8020 KB Output is correct
8 Correct 0 ms 8020 KB Output is correct
9 Correct 0 ms 8020 KB Output is correct
10 Correct 3 ms 8020 KB Output is correct
11 Correct 3 ms 8020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8152 KB Output is correct
2 Correct 3 ms 8152 KB Output is correct
3 Correct 6 ms 8152 KB Output is correct
4 Correct 9 ms 8152 KB Output is correct
5 Correct 9 ms 8284 KB Output is correct
6 Correct 9 ms 8284 KB Output is correct
7 Correct 9 ms 8284 KB Output is correct
8 Correct 9 ms 8284 KB Output is correct
9 Correct 9 ms 8284 KB Output is correct
10 Correct 9 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 10276 KB Output is correct
2 Correct 106 ms 10416 KB Output is correct
3 Correct 323 ms 13692 KB Output is correct
4 Correct 309 ms 13604 KB Output is correct
5 Correct 949 ms 20212 KB Output is correct
6 Correct 959 ms 20028 KB Output is correct
7 Correct 906 ms 19500 KB Output is correct
8 Correct 943 ms 20488 KB Output is correct
9 Correct 949 ms 18764 KB Output is correct
10 Correct 909 ms 20400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 10144 KB Output is correct
2 Correct 106 ms 10276 KB Output is correct
3 Correct 303 ms 13164 KB Output is correct
4 Correct 353 ms 13560 KB Output is correct
5 Correct 899 ms 18308 KB Output is correct
6 Execution timed out 1000 ms 19364 KB Execution timed out
7 Halted 0 ms 0 KB -