QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#94266 | #5379. Adjoin the Networks | DoubleAS | WA | 2ms | 3816kb | C++14 | 5.2kb | 2023-04-05 16:40:08 | 2023-04-05 16:40:09 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
//using namespace chrono;
typedef long long ll;
typedef unsigned long long ull;
typedef vector< int > vi;
typedef vector< ll > V;
typedef map<int, int > mp;
#define pb push_back
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define F first
#define S second
#define debug cout << -1 << endl;
#define REP(i, a, b) for(int i=a; i<b; i++)
#define f0r(i, n) for (int i = 0; i < n; ++i)
#define f0r1(i, n) for (int i = 1; i <= n; ++i)
#define r0f(i, n) for(int i=n-1; i>=0; i--)
#define r0f1(i, n) for(int i=n; i>=1; i--)
#define fore(a, x) for (auto& a : x)
#define fori(i, a, b) for (int i = (a); i < (b); ++i)
#define MP make_pair
#define UB upper_bound
#define LB lower_bound
#define nw cout << "\n"
#define issq(x) (((ll)(sqrt((x))))*((ll)(sqrt((x))))==(x))
#define rev(v) reverse(v.begin(),v.end())
#define asche cerr<<"Ekhane asche\n";
#define rev(v) reverse(v.begin(),v.end())
#define srt(v) sort(v.begin(),v.end())
#define grtsrt(v) sort(v.begin(),v.end(),greater<ll>())
#define all(v) v.begin(),v.end()
#define mnv(v) *min_element(v.begin(),v.end())
#define mxv(v) *max_element(v.begin(),v.end())
#define valid(tx,ty) (tx>=0 && tx<r && ty>=0 && ty<n)
#define one(x) __builtin_popcount(x)
#define dag cerr << "-----------------------------------------\n";
//#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
//#define pop pop_back
#define setPrec(x) cout << fixed << setprecision(x)
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
// int count(int l, int r, ordered_set &st) {
// return st.order_of_key(r+1) - st.order_of_key(l);
// }
// #define sz(a) (int)a.sz()
//#define fin cin
//#define fout cout
const int INF = 1e9;
const ll MOD = (ll)1e9+7;
const ll INFL = 1e18;
const ll mnINF = -1e18;
const double diff = 10e-6;
const int maxn = 1000002;
const int MX = 100002;
const double PI = acos(-1);
using namespace std;
#define MAX 10000002
const double bruh = .00000001;
// int dx[] = {-1, 0, 1};
// int dy[] = {-1, 0, 1};
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
// int dx[] = {0, 1};
// int dy[] = {-1, 0};
const int N = 1002;
map< int, vector< int > > adj;
int deg[100002];
int dist[100002];
int dist2[100002];
int find_dia(vector< int > cc, int s)
{
int mx = 0;
queue< int > q;
q.push(s);
for(auto x: cc) {
dist[x] = -1;
}
dist[s] = 0;
while(!q.empty()) {
int c = q.front();
q.pop();
mx = max(mx, dist[c]);
for(auto x: adj[c]) {
if(dist[x]==-1) {
dist[x] = dist[c]+1;
q.push(x);
}
}
}
return mx;
}
// int find_dia(int s) {
// }
vector< int > find_cc(int s)
{
vi ans;
queue< int > q;
q.push(s);
dist[s] = 0;
int last = 0, mx = 0;
while(!q.empty()) {
int c = q.front();
ans.pb(c);
q.pop();
if(mx<dist[c]) {
last = c;
mx = dist[c];
}
for(auto x: adj[c]) {
if(dist[x]==-1) {
dist[x] = dist[c] + 1;
q.push(x);
}
}
}
ans.pb(last);
return ans;
}
void solve()
{
int n, m;
cin >> n >> m;
f0r(i, m) {
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
deg[a]++; deg[b]++;
}
vi radii(3, 0);
int max_dia = 0;
memset(dist, -1, sizeof(dist));
f0r(i, n) {
// asche;
if(dist[i]!=-1) continue;
vector< int > cc = find_cc(i);
int dNode = cc.back();
cc.pop_back();
int dia = find_dia(cc, dNode);
max_dia = max(max_dia, dia);
// cout << dia << " ";
int rad = (dia+1)/2;
if(rad>radii[2]) {
radii[2] = rad;
}
srt(radii);
}
cout << max({max_dia, radii[0] + radii[1] + 1, radii[1] + radii[2] + 2}) << "\n";
}
void setIO(string name = "") { // name is nonempty for USACO file I/O
ios_base::sync_with_stdio(0); cin.tie(0); // see Fast Input & Output
// alternatively, cin.tie(0)->sync_with_stdio(0);
if (name.size()) {
freopen((name+".in").c_str(), "r", stdin); // see Input & Output
freopen((name+".out").c_str(), "w", stdout);
}
}
int32_t main()
{
//#ifndef ONLINE_JUDGE
// freopen("inputf.in", "r", stdin);
// freopen("outputf.in", "w", stdout);
//#endif
// setIO("gates");
// freopen("ariprog.in","r",stdin);
// freopen("ariprog.out","w",stdout);
FastIO;
// freopen("lightson.in","r",stdin);
// freopen("lightson.out","w",stdout);
int t;
t = 1;
// preCalc();
// memset(dp, -1, szof(dp));
// cin >> t;
// cin.ignore();
for(int i=0; i<t; i++) {
// if(i) nw;
// cout << "Case " << i+1 << ": ";
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3816kb
input:
6 4 0 1 0 2 3 4 3 5
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3720kb
input:
11 9 0 1 0 3 0 4 1 2 5 4 6 4 7 8 7 9 7 10
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 2ms
memory: 3720kb
input:
5 0
output:
2
result:
ok single line: '2'
Test #4:
score: -100
Wrong Answer
time: 2ms
memory: 3724kb
input:
6 3 0 1 2 3 4 5
output:
3
result:
wrong answer 1st lines differ - expected: '4', found: '3'