結果
| 問題 |
No.812 Change of Class
|
| コンテスト | |
| ユーザー |
hamuhei4869
|
| 提出日時 | 2019-03-18 19:46:10 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 2,291 bytes |
| コンパイル時間 | 1,998 ms |
| コンパイル使用メモリ | 176,876 KB |
| 実行使用メモリ | 20,160 KB |
| 最終ジャッジ日時 | 2024-06-12 05:17:28 |
| 合計ジャッジ時間 | 31,491 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 WA * 48 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const LL LINF = 1e18;
class Edge{
public:
int from,to;
LL cost;
Edge(int f, int t,LL c){
from = f;
to = t;
cost = c;
}
};
class UnionFind {
public:
vector<int> data;
UnionFind(int size) : data(size, -1) { }
bool connect(int x, int y) {
x = root(x); y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x;
}
return x != y;
}
bool same(int x, int y) {
return root(x) == root(y);
}
int root(int x) {
return data[x] < 0 ? x : data[x] = root(data[x]);
}
int size(int x) {
return -data[root(x)];
}
};
std::vector<LL> dijkstra(int s,int N,vector<Edge> E){
std::vector<LL> ans(N,LINF);
std::vector<std::vector<Edge>> Edges(N);
for(auto e : E){
Edges.at(e.from).push_back(e);
}
priority_queue<pair<LL,int>, std::vector<pair<LL,int>>, greater<pair<LL,int>>> que;
ans.at(s)= 0;
que.push(make_pair(0,s));
while(!que.empty()){
pair<LL,int> p = que.top();que.pop();
if(ans.at(p.second) < p.first)continue;
for(int i = 0;i < Edges.at(p.second).size();i++){
if(ans.at(p.second) + Edges.at(p.second).at(i).cost < ans.at(Edges.at(p.second).at(i).to)){
ans.at(Edges.at(p.second).at(i).to) = ans.at(p.second) + Edges.at(p.second).at(i).cost;
que.push(make_pair(ans.at(Edges.at(p.second).at(i).to),Edges.at(p.second).at(i).to));
}
}
}
return ans;
}
int main(){
int N,M;
cin >> N >> M;
UnionFind Uni(N);
vector<Edge> Edges;
for(int i = 0;i < M;i++){
int p,q;cin >> p >> q;
p--,q--;
Edges.push_back(Edge(p,q,1));
Edges.push_back(Edge(q,p,1));
Uni.connect(p, q);
}
int Q;cin >> Q;
for(int i = 0;i < Q;i++){
int A;cin >> A;
A--;
vector<LL> dis = dijkstra(A, N, Edges);
LL d = 0;
for(int j = 0;j < N;j++){
if(dis.at(j) != LINF)d = max(d, dis.at(j));
}
if(Uni.size(A) == 1)cout<<0<<" "<<0<<endl;
else cout<<Uni.size(A)-1<<" "<<(d-2)/2+(d-2)%2+1<<endl;
}
}
hamuhei4869