結果

問題 No.1719 Tree and Permutation
ユーザー tofu_dra2tofu_dra2
提出日時 2023-06-18 16:18:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 8,170 bytes
コンパイル時間 4,713 ms
コンパイル使用メモリ 264,868 KB
実行使用メモリ 8,948 KB
最終ジャッジ日時 2023-09-08 12:20:20
合計ジャッジ時間 7,622 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 192 ms
4,384 KB
testcase_02 AC 137 ms
4,376 KB
testcase_03 AC 209 ms
8,948 KB
testcase_04 AC 174 ms
8,148 KB
testcase_05 AC 182 ms
8,732 KB
testcase_06 AC 179 ms
8,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

typedef long long int ll;
typedef long double ld;

using namespace std;
using namespace atcoder;

#define inf 1010000000
#define llinf 1001000000000000000ll
#define pi 3.141592653589793238
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define rep1(i, n) for(ll i = 1; i <= (n); i++)
#define rep2(i,l,r) for(ll i = (l); i < (r); i++)
#define per(i, n) for(ll i = (n)-1; i >= 0; i--)
#define each(x, v) for (auto&& x : v)
#define rng(a) a.begin(),a.end()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define pob pop_back
#define st string
#define sz(x) (int)(x).size()
#define mems(x) memset(x, -1, sizeof(x));
#define pcnt __builtin_popcountll
#define _GLIBCXX_DEBUG

#define dame { puts("-1"); return 0;}
#define yes { puts("Yes"); return 0;}
#define no { puts("No"); return 0;}
#define ret(x) { cout<<(x)<<endl;}
#define rets(x) { cout<<(x)<< " ";}
#define Endl cout<<endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false;}

// 仮マクロ 便利だったら昇格
#define dump(x) { cout << #x << " = " << (x) << endl;}
#define bit(n) (1LL<<(n))
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end())

template <class T = ll>
inline T in(){ T x; cin >> x; return (x);}

#define vcin(x,n) {for(ll loop=0; loop<(n); loop++) cin>>x[loop];}

// ここまで仮マクロ

// srand((unsigned)time(NULL)); rand()を用いる際にmainの頭に置く
// clock()/CLOCKS_PER_SEC 秒数を知りたいときに用いる

#define mod 998244353
using mint = modint998244353;

/*

#define mod 1000000007
using mint = modint1000000007;

*/

vector<ll> dx={1,0,-1,0};
vector<ll> dy={0,1,0,-1};

using pl = pair<ll,ll>;
using ppl = pair<ll,pl>;
using V = vector<ll>;
using Graph = vector<vector<ll>>;
// G.assign(n, vector<ll>()); グローバル変数にGを置く時に置く

// 関数を置くのはここ以下

template <typename T>
struct edge {
  int src, to;
  T cost;

  edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {}
  edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};
template <typename T>
using Edges = vector<edge<T>>;
template <typename T>
using WeightedGraph = vector<Edges<T>>;
using UnweightedGraph = vector<vector<int>>;

// Input of (Unweighted) Graph
UnweightedGraph graph(int N, int M = -1, bool is_directed = false,
                      bool is_1origin = true) {
  UnweightedGraph g(N);
  if (M == -1) M = N - 1;
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    if (is_1origin) x--, y--;
    g[x].push_back(y);
    if (!is_directed) g[y].push_back(x);
  }
  return g;
}

// Input of Weighted Graph
template <typename T>
WeightedGraph<T> wgraph(int N, int M = -1, bool is_directed = false,
                        bool is_1origin = true) {
  WeightedGraph<T> g(N);
  if (M == -1) M = N - 1;
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    T c;
    cin >> c;
    if (is_1origin) x--, y--;
    g[x].emplace_back(x, y, c);
    if (!is_directed) g[y].emplace_back(y, x, c);
  }
  return g;
}

// Input of Edges
template <typename T>
Edges<T> esgraph(int N, int M, int is_weighted = true, bool is_1origin = true) {
  Edges<T> es;
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    T c;
    if (is_weighted)
      cin >> c;
    else
      c = 1;
    if (is_1origin) x--, y--;
    es.emplace_back(x, y, c);
  }
  return es;
}

// Input of Adjacency Matrix
template <typename T>
vector<vector<T>> adjgraph(int N, int M, T INF, int is_weighted = true,
                           bool is_directed = false, bool is_1origin = true) {
  vector<vector<T>> d(N, vector<T>(N, INF));
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    T c;
    if (is_weighted)
      cin >> c;
    else
      c = 1;
    if (is_1origin) x--, y--;
    d[x][y] = c;
    if (!is_directed) d[y][x] = c;
  }
  return d;
}

// 一般のグラフのstからの距離!!!!
// unvisited nodes : d = -1
vector<int> Depth(const UnweightedGraph &g, int start = 0) {
  int n = g.size();
  vector<int> ds(n, -1);
  ds[start] = 0;
  queue<int> q;
  q.push(start);
  while (!q.empty()) {
    int c = q.front();
    q.pop();
    int dc = ds[c];
    for (auto &d : g[c]) {
      if (ds[d] == -1) {
        ds[d] = dc + 1;
        q.push(d);
      }
    }
  }
  return ds;
}

// Depth of Rooted Weighted Tree
// unvisited nodes : d = -1
template <typename T>
vector<T> Depth(const WeightedGraph<T> &g, int start = 0) {
  vector<T> d(g.size(), -1);
  auto dfs = [&](auto rec, int cur, T val, int par = -1) -> void {
    d[cur] = val;
    for (auto &dst : g[cur]) {
      if (dst == par) continue;
      rec(rec, dst, val + dst.cost, cur);
    }
  };
  dfs(dfs, start, 0);
  return d;
}

// Diameter of Tree
// return value : { {u, v}, length }
pair<pair<int, int>, int> Diameter(const UnweightedGraph &g) {
  auto d = Depth(g, 0);
  int u = max_element(begin(d), end(d)) - begin(d);
  d = Depth(g, u);
  int v = max_element(begin(d), end(d)) - begin(d);
  return make_pair(make_pair(u, v), d[v]);
}

// Diameter of Weighted Tree
// return value : { {u, v}, length }
template <typename T>
pair<pair<int, int>, T> Diameter(const WeightedGraph<T> &g) {
  auto d = Depth(g, 0);
  int u = max_element(begin(d), end(d)) - begin(d);
  d = Depth(g, u);
  int v = max_element(begin(d), end(d)) - begin(d);
  return make_pair(make_pair(u, v), d[v]);
}

// nodes on the path u-v ( O(N) )
template <typename G>
vector<int> Path(G &g, int u, int v) {
  vector<int> ret;
  int end = 0;
  auto dfs = [&](auto rec, int cur, int par = -1) -> void {
    ret.push_back(cur);
    if (cur == v) {
      end = 1;
      return;
    }
    for (int dst : g[cur]) {
      if (dst == par) continue;
      rec(rec, dst, cur);
      if (end) return;
    }
    if (end) return;
    ret.pop_back();
  };
  dfs(dfs, u);
  return ret;
}

template <typename T>
vector<T> dijkstra(WeightedGraph<T> &g, int start = 0) {
  using P = pair<T, int>;
  int N = (int)g.size();
  vector<T> d(N, T(-1));
  priority_queue<P, vector<P>, greater<P> > Q;
  d[start] = 0;
  Q.emplace(0, start);
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    int cur = p.second;
    if (d[cur] < p.first) continue;
    for (auto dst : g[cur]) {
      if (d[dst] == T(-1) || d[cur] + dst.cost < d[dst]) {
        d[dst] = d[cur] + dst.cost;
        Q.emplace(d[dst], dst);
      }
    }
  }
  return d;
}

// edge<T>	型Tの重みが付いた辺
// Edges<T>	型Tの重みが付いた辺のリスト
// WeightedGraph<T>	型Tの重みが付いた(有向/無向)グラフ
// UnweightedGraph	重みなし(有向/無向)グラフ

// graph(N, M(-1の時N-1), bool is_directed = false, bool is_1origin = true)
//   重みなしのグラフを標準入力から読み込み、UnweightedGraph型で返す。
//   ex) graph(n,,,) であればN頂点の無向木
// wgraph(...) = 有向木
// esgraph,adjgraph = 隣接リスト,隣接行列を返す

// - 機能 - ( だいたいO(N) )
// vector<int> Depth(UnweightedGraph g, int s = 0)
//   sからの最短距離
// vector<T> Depth(weightedGraph<T> g, int s = 0)
//   (重み付き根つき木限定) sからの距離
// pair<pair<int, int>, int> Diameter(UnweightedGraph g)
// pair<pair<int, int>, T> Diameter<T>(WeightedGraph<T> g)
//   木の直径の両端,長さを返す
// vector<int> Path<UnweightedGraph>(G &g, int u, int v)
// vector<int> Path<WeightedGraph>(G &g, int u, int v)
//   (両端含む)パスの頂点列を返す
// vector<T> dijkstra(g, s = 0) 
//   ダイクストラ法 (O(ElogV))
//   計算量:O(ElogV)
//   届かなかったら-1


int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  ll t = in();
  rep(_,t){
    ll n = in();
    auto g = graph(n);
    ll cnt = 0;
    rep(i,n){
      if(g[i].size()==1) cnt++;
    }
    cnt *= 2;
    if(cnt>=n) ret("Yes")
    else ret("No")
  }
}
0