Submission #809066
Source Code Expand
#include<bits/stdc++.h>
#define CLR(a,b) memset(a,b,sizeof(a))
#define LL long long
using namespace std;
vector<LL>graph[3005];
vector<LL>cost[3005];
bool visited[3005];
LL dis[3005];
void BFS(LL x, LL &y)
{
CLR(visited,false);
CLR(dis,0);
queue<LL>q;
q.push(x);
visited[x]=true;
dis[x]=0;
LL maxi = -1;
while(!q.empty())
{
LL u = q.front();
q.pop();
for(LL i=0; i<graph[u].size(); i++)
{
LL v = graph[u][i];
LL c = cost[u][i];
if(!visited[v])
{
visited[v]=true;
dis[v] = dis[u]+cost[u][i];
if(dis[v]>maxi)
{
maxi=dis[v];
y = v;
}
q.push(v);
}
}
}
}
void BFS2(LL x)
{
memset(dis, 0, sizeof dis);
memset(visited, 0, sizeof visited);
queue<LL>q;
q.push(x);
visited[x] = true;
dis[x] = 0;
while(!q.empty())
{
LL u = q.front();
q.pop();
for(LL i=0; i<graph[u].size(); i++)
{
LL v = graph[u][i];
if(!visited[v])
{
visited[v] = true;
dis[v] = dis[u] + cost[u][i];
q.push(v);
}
}
}
}
int main()
{
LL test, K;
LL nodes;
scanf("%lld %lld", &nodes, &K);
for(LL i=1; i<nodes; i++)
{
LL x, y;
scanf("%lld %lld", &x, &y);
graph[x].push_back(y);
graph[y].push_back(x);
cost[x].push_back(1);
cost[y].push_back(1);
}
LL s, t, w;
BFS(1,s);
BFS(s,t);
// cout<<"S = "<<t<<endl;
BFS2(t);
// cout<<s<<' '<<t<<' '<<w<<endl;
LL cnt1 = 0, cnt2 = 0;
for(LL i=1; i<=nodes; i++)
{
// cout<<dis[i]<<endl;
if(dis[i]>K) cnt1++;
}
// cout<<"S = "<<s<<endl;
BFS2(s);
// cout<<s<<' '<<t<<' '<<w<<endl;
cnt2 = 0;
for(LL i=1; i<=nodes; i++)
{
// cout<<dis[i]<<endl;
if(dis[i]>K) cnt2++;
}
printf("%lld", min(cnt1, cnt2));
return 0;
}
Submission Info
Submission Time |
|
Task |
C - Shorten Diameter |
User |
ssavi |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
2247 Byte |
Status |
WA |
Exec Time |
6 ms |
Memory |
640 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:76:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld", &nodes, &K);
^
./Main.cpp:80:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld", &x, &y);
^
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 600 |
Status |
|
|
Set Name |
Test Cases |
Sample |
sample-01.txt, sample-02.txt |
All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt |
Case Name |
Status |
Exec Time |
Memory |
000.txt |
AC |
4 ms |
384 KB |
001.txt |
AC |
4 ms |
384 KB |
002.txt |
AC |
4 ms |
384 KB |
003.txt |
AC |
4 ms |
384 KB |
004.txt |
AC |
4 ms |
384 KB |
005.txt |
AC |
4 ms |
384 KB |
006.txt |
AC |
4 ms |
384 KB |
007.txt |
AC |
4 ms |
384 KB |
008.txt |
AC |
4 ms |
384 KB |
009.txt |
AC |
6 ms |
512 KB |
010.txt |
AC |
6 ms |
640 KB |
011.txt |
AC |
6 ms |
512 KB |
012.txt |
WA |
6 ms |
512 KB |
013.txt |
AC |
6 ms |
640 KB |
014.txt |
WA |
6 ms |
512 KB |
015.txt |
WA |
6 ms |
512 KB |
016.txt |
WA |
6 ms |
640 KB |
017.txt |
WA |
6 ms |
512 KB |
018.txt |
AC |
5 ms |
384 KB |
019.txt |
AC |
4 ms |
384 KB |
020.txt |
AC |
5 ms |
512 KB |
021.txt |
WA |
6 ms |
512 KB |
022.txt |
WA |
5 ms |
512 KB |
023.txt |
WA |
4 ms |
384 KB |
024.txt |
WA |
5 ms |
512 KB |
025.txt |
WA |
4 ms |
384 KB |
026.txt |
WA |
6 ms |
512 KB |
027.txt |
AC |
6 ms |
512 KB |
028.txt |
AC |
6 ms |
512 KB |
029.txt |
AC |
6 ms |
512 KB |
030.txt |
AC |
6 ms |
640 KB |
031.txt |
AC |
6 ms |
640 KB |
sample-01.txt |
AC |
4 ms |
384 KB |
sample-02.txt |
AC |
4 ms |
384 KB |