QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#416#230012#7640. Colorful Cyclesucup-team1004ucup-team987Failed.2023-10-30 16:27:352023-10-30 16:27:36

Details

Extra Test:

Accepted
time: 4ms
memory: 34144kb

input:

1
5 6
1 2 2
1 3 1
1 4 1
2 5 2
3 5 1
4 5 3

output:

Yes

result:

ok YES

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#230012#7640. Colorful Cyclesucup-team987#Compile Error//C++204.8kb2023-10-28 17:24:392024-10-14 08:01:26

answer

#include<iostream>
#include<algorithm>
#include<vector>
#include<cassert>
using namespace std;
#line 2 "graph/graph-template.hpp"

/**
 * @brief Graph Template(繧ー繝ゥ繝輔ユ繝ウ繝励Ξ繝シ繝・
 */
template< typename T = int >
struct Edge {
  int from, to;
  int idx;

  Edge() = default;

  Edge(int from, int to, int idx = -1) : from(from), to(to), idx(idx) {}

  operator int() const { return to; }
};

template< typename T = int >
struct Graph {
  vector< vector< Edge< T > > > g;
  int es;

  Graph() = default;

  explicit Graph(int n) : g(n), es(0) {}

  size_t size() const {
    return g.size();
  }

  void add_directed_edge(int from, int to) {
    g[from].emplace_back(from, to, es++);
  }

  void add_edge(int from, int to) {
    g[from].emplace_back(from, to, es);
    g[to].emplace_back(to, from, es++);
  }

  inline vector< Edge< T > > &operator[](const int &k) {
    return g[k];
  }

  inline const vector< Edge< T > > &operator[](const int &k) const {
    return g[k];
  }
};

template< typename T = int >
using Edges = vector< Edge< T > >;
#line 2 "graph/others/low-link.hpp"

#line 4 "graph/others/low-link.hpp"

/**
 * @brief Low Link(讖・髢「遽€轤ケ)
 * @see http://kagamiz.hatenablog.com/entry/2013/10/05/005213
 * @docs docs/low-link.md
 */
template< typename T = int >
struct LowLink : Graph< T > {
public:
  using Graph< T >::Graph;
  vector< int > ord, low, articulation;
  vector< Edge< T > > bridge;
  using Graph< T >::g;

  virtual void build() {
    used.assign(g.size(), 0);
    ord.assign(g.size(), 0);
    low.assign(g.size(), 0);
    int k = 0;
    for(int i = 0; i < (int) g.size(); i++) {
      if(!used[i]) k = dfs(i, k, -1);
    }
  }

  explicit LowLink(const Graph< T > &g) : Graph< T >(g) {}

private:
  vector< int > used;

  int dfs(int idx, int k, int par) {
    used[idx] = true;
    ord[idx] = k++;
    low[idx] = ord[idx];
    bool is_articulation = false, beet = false;
    int cnt = 0;
    for(auto &to : g[idx]) {
      if(to == par && !exchange(beet, true)) {
        continue;
      }
      if(!used[to]) {
        ++cnt;
        k = dfs(to, k, idx);
        low[idx] = min(low[idx], low[to]);
        is_articulation |= par >= 0 && low[to] >= ord[idx];
        if(ord[idx] < low[to]) bridge.emplace_back(to);
      } else {
        low[idx] = min(low[idx], ord[to]);
      }
    }
    is_articulation |= par == -1 && cnt > 1;
    if(is_articulation) articulation.push_back(idx);
    return k;
  }
};
#line 3 "graph/connected-components/bi-connected-components.hpp"

template< typename T = int >
struct BiConnectedComponents : LowLink< T > {
public:
  using LowLink< T >::LowLink;
  using LowLink< T >::g;
  using LowLink< T >::ord;
  using LowLink< T >::low;

  vector< vector< Edge< T > > > bc;

  void build() override {
    LowLink< T >::build();
    used.assign(g.size(), 0);
    for(int i = 0; i < (int)used.size(); i++) {
      if(!used[i]) dfs(i, -1);
    }
  }

  explicit BiConnectedComponents(const Graph< T > &g) : Graph< T >(g) {}

private:
  vector< int > used;
  vector< Edge< T > > tmp;

  void dfs(int idx, int par) {
    used[idx] = true;
    bool beet = false;
    for(auto &to : g[idx]) {
      if(to == par && !exchange(beet, true)) continue;
      if(!used[to] || ord[to] < ord[idx]) {
        tmp.emplace_back(to);
      }
      if(!used[to]) {
        dfs(to, idx);
        if(low[to] >= ord[idx]) {
          bc.emplace_back();
          for(;;) {
            auto e = tmp.back();
            bc.back().emplace_back(e);
            tmp.pop_back();
            if(e.idx == to.idx) break;
          }
        }
      }
    }
  }
};
int U[1<<20],V[1<<20],COL[1<<20];
vector<pair<int,int> >G[1<<20];
int TM[1<<20];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T;cin>>T;
	for(;T--;)
	{
		int N,M;
		cin>>N>>M;
		BiConnectedComponents<>B(N);
		for(int i=0;i<M;i++)
		{
			cin>>U[i]>>V[i]>>COL[i];
			U[i]--,V[i]--;
			B.add_edge(U[i],V[i]);
		}
		B.build();
		for(int i=0;i<N;i++)TM[i]=-1;
		bool ans=false;
		for(int i=0;i<B.bc.size();i++)
		{
			vector<int>VS;
			int t=0;
			for(int j=0;j<B.bc[i].size();j++)
			{
				int id=B.bc[i][j].idx;
				if(TM[U[id]]<i)
				{
					TM[U[id]]=i;
					VS.push_back(U[id]);
				}
				if(TM[V[id]]<i)
				{
					TM[V[id]]=i;
					VS.push_back(V[id]);
				}
				G[U[id]].push_back(make_pair(V[id],COL[id]));
				G[V[id]].push_back(make_pair(U[id],COL[id]));
				t|=1<<COL[id]-1;
			}
			if(t==7)
			{
				int cnt=0;
				for(int v:VS)
				{
					int x=0;
					for(pair<int,int>e:G[v])x|=1<<e.second-1;
					if(x==1||x==2||x==4);
					else cnt++;
				}
				if(cnt>=3)ans=true;
			}
			for(int v:VS)G[v].clear();
		}
		cout<<(ans?"Yes\n":"No\n");
	}
}