#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast","unroll-loops")
struct bfstyp{
int ind;
int val;
};
void prim(int poc,int n,vector<vector<int>>& gra,int& najv,int& najvkol){
vector<int> dist(n,-1);
dist[poc]=0;
queue<bfstyp> bfslis={};
bfslis.push(bfstyp{poc,0});
int najv2=0;
while (bfslis.size()>0){
int ind=bfslis.front().ind;
int val=bfslis.front().val;
najv2=max(najv2,val);
//` cout << ind << "\n";
bfslis.pop();
for (int i=0;i<gra[ind].size();i++){
// cout << i << "\n";
int ind2=gra[ind][i];
if (dist[ind2]!=-1){
continue;
}
dist[ind2]=val+1;
bfslis.push(bfstyp{ind2,val+1});
}
}
vector<vector<int>> lispom(najv2+3,vector<int>{});
// cout << "\n";
for (int i=0;i<n;i++){
// cout << dist[i] << "\n";
lispom[dist[i]].push_back(i);
//lis.push_back({dist[i],i});
}
vector<pair<int,int>> lis={};
for (int i=0;i<najv2;i++){
for (int j=0;j<lispom[i].size();j++){
lis.push_back({i,lispom[i][j]});
}
}
// cout << "\n";
//sort(lis.begin(),lis.end());
vector<int> dp(n,0);
dp[poc]=1;
for (int i=0;i<n;i++){
int ind=lis[i].second;
if (dp[ind]==0){
continue;
}
for (int j=0;j<gra[ind].size();j++){
int ind2=gra[ind][j];
if (dist[ind2]==dist[ind]+1){
dp[ind2]+=dp[ind];
if (dist[ind2]==najv){
najvkol+=dp[ind];
}
else if (najv<dist[ind2]){
najv=dist[ind2];
najvkol=dp[ind];
}
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<vector<int>> gra(n,vector<int>{});
for (int i=0;i<n;i++){
int unos;
int unos2;
cin >> unos >> unos2;
gra[--unos].push_back(--unos2);
gra[unos2].push_back(unos);
}
int najv=0;
int najvkol=0;
for (int i=0;i<n;i++){
prim(i,n,gra,najv,najvkol);
}
// cout << najv << "\n";
cout << najvkol/2 << "\n";
}
Compilation message
shymbulak.cpp: In function 'void prim(int, int, std::vector<std::vector<int> >&, int&, int&)':
shymbulak.cpp:24:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for (int i=0;i<gra[ind].size();i++){
| ~^~~~~~~~~~~~~~~~
shymbulak.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int j=0;j<lispom[i].size();j++){
| ~^~~~~~~~~~~~~~~~~
shymbulak.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int j=0;j<gra[ind].size();j++){
| ~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
30 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1542 ms |
8252 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |