結果

問題 No.583 鉄道同好会
ユーザー nanophoto12nanophoto12
提出日時 2017-10-27 23:03:46
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,892 bytes
コンパイル時間 965 ms
コンパイル使用メモリ 99,372 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-05-01 17:22:26
合計ジャッジ時間 4,511 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
10,624 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cmath>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <functional>
#include <queue>
#include <iostream>
#include <string.h>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <cstdint>
#include <climits>
#include <unordered_set>
#include <sstream>
#include <stack>

using namespace std;

typedef long long int ll;
typedef pair<int,int> pii;
typedef tuple<ll,ll,ll> t3;

using namespace std;
int n,m;
vector<vector<pii>> g;

vector<int> used;

int dfs(int from, int current)
{
    for(auto e : g[from])
    {
        auto index = e.second;
        if(used[index]) continue;
        auto next = e.first;
        used[index] = true;
        int count = dfs(next, current + 1);
        used[index] = false;
        if(count == m)
        {
            return m;
        }
    }
    return current;
}


bool dfs()
{
    int index = -1;
    int valid = -1;
    for(int i = 0;i < n;i++)
    {
        if(g[i].size() == 1)
        {
            index = i;
            break;
        }
        else if(g[i].size() > 1)
        {
            valid = i;
        }
    }
    if(index >= 0)
    {
        int to = g[index][0].first;
        used[g[index][0].second] = true;
        int r = dfs(to, 1);
        if(r == m)
        {
            return true;
        }
        return false;
    }
    else
    {
        int to = g[valid][0].first;
        used[g[valid][0].second] = true;
        int r = dfs(to, 1);
        if(r == m)
        {
            return true;
        }
        return false;
    }
}

int main()
{
    cin >> n >> m;
    g.resize(n);
    used.assign(m, 0);
    for(int i = 0;i < m;i++)
    {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b,i);
        g[b].emplace_back(a,i);
    }
    
    if(dfs())
    {
        cout << "YES" << endl;
        return 0;
    }
    cout << "NO" << endl;
    return 0;
}
0