제출 #1053751

#제출 시각아이디문제언어결과실행 시간메모리
1053751nightfalStations (IOI20_stations)C++17
0 / 100
513 ms948 KiB
// #include "stations.h"
#include <cstdio>
#include <iostream>
#include <cassert>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

static int max_label = 0;
static int r, n, k, q;
static std::vector<int> u, v, labels, answers;
static std::map<int, int> reverse_labels;
static std::vector<std::vector<int>> adjlist;
static int s, t, w;
static std::vector<int> c;

template <typename T> void print(T elem) {cout << elem << " ";}
template <typename T> void print(vector<T> &v) {for(auto elem: v) print(elem); cout << endl;};
template <typename T> void print(vector<vector<T>> &v) {for(auto elem: v) print(elem); cout << endl;};

bool subtask1(vector<vector<int>> &g) {
    int n = g.size();
    for(auto elem: g)
        if (elem.size() > 2) return false;
    return true;
}
bool subtask2(vector<int> &u, vector<int> &v) {
    int m = u.size();
    for(int i=0; i<m; i++)
        if (!(u[i]==i+1 && v[i]==i/2 || u[i]==i/2 && v[i]==i+1))
            return false;
    return true;
}
bool subtask3(vector<vector<int>> &g) {
    int n=g.size(), cnt=0;
    for(auto elem: g)
        if(elem.size()>2) cnt++;
    if (cnt<=1) return true;
    return false;
}
void genLabels(int key, int node, vector<int> &labels, vector<vector<int>> &g) {
    labels[node]=key*1000+1;
    for(int i=2; i<1000; i++) {
        bool terminal = true;
        for(int next: g[node]) {
            // cout << "next:" << next << endl;
            if(labels[next]==-1) {labels[next] = 1000*key+i; node = next; terminal=false;}
        }
        if (terminal) return;
    }
}
std::vector<int> label(int n, int k, std::vector<int> u, std::vector<int> v) {
	std::vector<int> labels(n,-1);
    vector<vector<int>> g(n);
    for (int i=0; i<n-1; i++) {
        g[u[i]].push_back(v[i]);
        g[v[i]].push_back(u[i]);
    }
    // print(g);
    if (subtask1(g)) {
        int node = 0;
        for (int i=0; i<n; i++)
            if(g[i].size()==1) node = i;
        int prev = node;
        for (int i=0; i<n; i++) {
            labels[node] = i;
            for(int next: g[node])
                if(labels[next]== -1) {node = next;}
        }
    }
    else if (subtask2(u,v)) {
        for(int i=0; i<n; i++)
            labels[i] = i;
    }
    else if (subtask3(g)) {
        int root = 0;
        for(int i=0; i<n; i++)
            if(g[i].size() > 2)  {root = i; break;}
            labels[root] = 0;
        // print(labels);
        // print(g);
        for(int i=0; i<g[root].size(); i++) {
            int child = g[root][i];
            genLabels(i, child, labels, g);
        }
    }
    // print(labels);
	return labels;
}
bool ancestor(int s, int t) {
    if (s==0) return true;
    while (t) {
        if (s==t) return true;
        t = (t-1)>>1;
    }
    return false;
}
int find_next_station(int s, int t, std::vector<int> c) {
    int subtask = 2;
    for(int i=0; i<c.size(); i++)
        if(c[i]>=1000) {subtask = 3; break;}

    if (subtask==2) 
        for(int i=0; i<c.size(); i++)
            if(!(c[i]==2*s+1 || c[i]==2*s+2 || c[i]==s/2)) {subtask = 1; break;}

    if (subtask==1) {
        for(int next: c)
            if (s<t && s<next) return next;
            else if (s>t && s>next) return next;
        }
    else if (subtask==2) {
        if (ancestor(2*s+1,t)) return 2*s+1;
        else if (ancestor(2*s+2,t)) return 2*s+2;
        else return (s-1)/2;
    }
    else if (subtask==3) {
        if (s==0) return (t/1000)*1000+1;
        if (s/1000 == t/1000) {
            if (s<t) return s+1;
            else return s-1;
        }
        else { 
            if (s%1000==1) return 0;
            else return s-1;
        }
    }
}

컴파일 시 표준 에러 (stderr) 메시지

stations.cpp: In function 'bool subtask1(std::vector<std::vector<int> >&)':
stations.cpp:23:9: warning: unused variable 'n' [-Wunused-variable]
   23 |     int n = g.size();
      |         ^
stations.cpp: In function 'bool subtask2(std::vector<int>&, std::vector<int>&)':
stations.cpp:31:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   31 |         if (!(u[i]==i+1 && v[i]==i/2 || u[i]==i/2 && v[i]==i+1))
stations.cpp: In function 'bool subtask3(std::vector<std::vector<int> >&)':
stations.cpp:36:9: warning: unused variable 'n' [-Wunused-variable]
   36 |     int n=g.size(), cnt=0;
      |         ^
stations.cpp: In function 'std::vector<int> label(int, int, std::vector<int>, std::vector<int>)':
stations.cpp:65:13: warning: unused variable 'prev' [-Wunused-variable]
   65 |         int prev = node;
      |             ^~~~
stations.cpp:78:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   78 |         for(int i=0; i<n; i++)
      |         ^~~
stations.cpp:80:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   80 |             labels[root] = 0;
      |             ^~~~~~
stations.cpp:83:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         for(int i=0; i<g[root].size(); i++) {
      |                      ~^~~~~~~~~~~~~~~
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:101:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |     for(int i=0; i<c.size(); i++)
      |                  ~^~~~~~~~~
stations.cpp:105:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |         for(int i=0; i<c.size(); i++)
      |                      ~^~~~~~~~~
stations.cpp:129:1: warning: control reaches end of non-void function [-Wreturn-type]
  129 | }
      | ^
stations.cpp: At global scope:
stations.cpp:15:18: warning: 'w' defined but not used [-Wunused-variable]
   15 | static int s, t, w;
      |                  ^
stations.cpp:15:15: warning: 't' defined but not used [-Wunused-variable]
   15 | static int s, t, w;
      |               ^
stations.cpp:15:12: warning: 's' defined but not used [-Wunused-variable]
   15 | static int s, t, w;
      |            ^
stations.cpp:11:21: warning: 'q' defined but not used [-Wunused-variable]
   11 | static int r, n, k, q;
      |                     ^
stations.cpp:11:18: warning: 'k' defined but not used [-Wunused-variable]
   11 | static int r, n, k, q;
      |                  ^
stations.cpp:11:15: warning: 'n' defined but not used [-Wunused-variable]
   11 | static int r, n, k, q;
      |               ^
stations.cpp:11:12: warning: 'r' defined but not used [-Wunused-variable]
   11 | static int r, n, k, q;
      |            ^
stations.cpp:10:12: warning: 'max_label' defined but not used [-Wunused-variable]
   10 | static int max_label = 0;
      |            ^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...