結果

問題 No.845 最長の切符
ユーザー yuyayuya
提出日時 2019-06-28 22:38:46
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,169 bytes
コンパイル時間 3,032 ms
コンパイル使用メモリ 87,788 KB
実行使用メモリ 7,424 KB
最終ジャッジ日時 2023-09-14 22:23:41
合計ジャッジ時間 6,248 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,312 KB
testcase_01 WA -
testcase_02 AC 4 ms
7,392 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>
#include <utility>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iomanip>
#include <cstdio>
#include <map>
#include <list>

using namespace std;

typedef long long Int;
typedef pair<Int,Int> P;

#define rep(i,x) for(Int i = 0; i < (Int)(x); i++)
#define rrep(i,x) for(Int i = ((Int)(x) - 1); i >= 0; i--)
#define _upgrade ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define pb push_back
#define MX 100010


template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

//ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}

Int dx[4] = {1,0,-1,0};
Int dy[4] = {0,1,0,-1};
const Int INF = 1001002003004005006ll;
const Int mod = 1e9 + 7;
const double PI = 3.14159265358979323846;


// ****************************************CODE***************************************//


struct UnionFind{
        vector<int> par;
        
        UnionFind(int n) : par(n){
                for(int i = 0; i < n; i++) par[i] = i;
        }

        int root(int x){
                if(par[x] == x) return x;
                return par[x] = root(par[x]);
        }

        void Union(int x, int y){
                int nx = root(x);
                int ny = root(y);
                if(nx == ny) return;
                par[nx] = ny;
        }

        bool same(int x, int y){
                int nx = root(x);
                int ny = root(y);
                return nx == ny;
        }
};




int n,m;
int graph[1010][1010];
bool visited[1010];

int dfs(int x){
        if(x == n) return 0;

        int ans = 0;
        for(int i = 0; i < n; i++){
                int ret = 0;
                if(graph[x][i] == -1) continue;
                if(visited[i]) continue;

                ret += graph[x][i];
                visited[i] = true;
                ret += dfs(i);
                ans = max(ret,ans);
                visited[i] = false;
        }

        return ans;
}



int main(){
        cin >> n >> m;
        memset(graph,-1,sizeof(graph));
        memset(visited,false,sizeof(visited));

        UnionFind tree(n);

        rep(i,m){
                int a,b,c;
                cin >> a >> b >> c;
                a--;
                b--;
                graph[a][b] = graph[b][a] = c;
                tree.Union(a,b);
        }
        visited[0] = true;
        int ans = dfs(0);
        vector<int> check;
        check.push_back(0);
        for(int j = 0; j < n; j++){
                bool ok = true;
                for(int i = 0; i < check.size(); i++){
                        if(i==j) ok = false;
                        if(tree.same(j,check[i])) ok = false;
                }
                if(ok){
                        ans += dfs(j);
                        check.push_back(j);
                }
        }
        cout << ans << endl;
        return 0;
}


























0