ondskan   2년 전

1번 코드는 통과하고

2번은 시간초과가 나오는데 

내부로직에 큰 차이가 없다고 생각하는데 왜 이런 결과가 나오는지 모르겠습니다.

1번 코드
#include
#include
#include
#include
using namespace std;
int n, m;
int a, b;
int dist[10001];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);

cin >> n >> m;
vector com[10001];

for (int i = 0; i < m; i++)
{
cin >> a >> b;
com[b].push_back(a);
}
queue q;
for (int i = 1; i <= n; i++)
{
vector vis(n + 1, false); // 방문 배열
q.push(i);
int cnt = 0;
vis[i] = true;
while (!q.empty())
{
int cur = q.front();
q.pop();
cnt++;
for (auto nxt : com[cur])
{
if (!vis[nxt])
{
q.push(nxt);
vis[nxt] = true;

}
}

}
dist[i] = cnt;
}
int mx = 0;
for (int i = 1; i <= n; i++)
{
mx = max(mx, dist[i]);
}
for (int i = 1; i <= n; i++)
{
if (dist[i] == mx)
{
cout << i << " ";
}
}
return 0;
}

<2번 코드>
#include
#include
#include
#include
using namespace std;
int n, m;
int a, b;
bool vis[10001];
int dist[10001];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);

cin >> n >> m;
vector com[10001];

for (int i = 0; i < m; i++)
{
cin >> a >> b;
com[b].push_back(a);
}
queue q;
for (int i = 1; i <= n; i++)
{
q.push(i);
int cnt = 0;
fill(vis, vis + n + 1, false);
while (!q.empty())
{
int cur = q.front();
q.pop();
cnt++;
vis[cur] = true;
for (auto nxt : com[cur])
{
if (!vis[nxt])
{
q.push(nxt);
}
}

}
dist[i] = cnt;
}
int mx = 0;
for (int i = 1; i <= n; i++)
{
mx = max(mx, dist[i]);
}
for (int i = 1; i <= n; i++)
{
if (dist[i] == mx)
{
cout << i << " ";
}
}
return 0;
}

siyamaki   2년 전

코드는 하단의 소스추가를 누르면 나오는 곳에 올려주세요

댓글을 작성하려면 로그인해야 합니다.