QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#229732 | #7640. Colorful Cycles | ucup-team987# | RE | 3ms | 34360kb | C++20 | 5.3kb | 2023-10-28 16:46:09 | 2023-10-28 16:46:09 |
Judging History
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 dfs1(int u,int pr,int Y,int c)
{
if(u==Y)return c;
int cnt=0;
for(pair<int,int>e:G[u])if(e.first!=pr)
{
cnt++;
int t=dfs1(e.first,u,Y,c|1<<e.second-1);
c|=t;
}
assert(cnt==1);
return c;
}
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];
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 X=-1,Y=-1,Z=-1;
for(int v:VS)
{
assert(G[v].size()>=2);
if(G[v].size()==2)continue;
if(X==-1)X=v;
else if(Y==-1)Y=v;
else Z=v;
}
if(X==-1||Z!=-1)
{
ans=true;
}
else
{
assert(Y!=-1);
for(pair<int,int>e:G[X])
{
int t=dfs1(e.first,X,Y,1<<e.second-1);
if(t==1||t==2||t==4);
else
{
ans=true;
break;
}
}
}
}
for(int v:VS)G[v].clear();
}
cout<<(ans?"Yes\n":"No\n");
}
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 34360kb
input:
2 3 3 1 2 3 2 3 1 1 3 2 5 6 1 2 1 2 3 1 1 3 2 3 4 3 3 5 3 4 5 3
output:
Yes No
result:
ok 2 token(s): yes count is 1, no count is 1
Test #2:
score: -100
Runtime Error
input:
100000 7 10 7 2 2 6 4 2 6 1 2 7 1 3 3 4 1 6 7 1 2 6 3 3 1 2 5 3 1 2 1 1 7 10 5 7 3 7 1 1 4 6 3 6 3 1 3 4 3 4 2 2 3 2 3 1 3 3 3 7 1 1 4 2 7 10 5 6 3 3 5 2 7 2 3 7 3 3 1 2 2 4 3 2 7 4 2 6 1 2 2 6 1 7 5 2 7 10 7 1 3 7 5 3 6 4 1 7 6 1 1 4 1 3 4 2 2 7 2 1 3 1 3 5 3 5 1 3 7 10 6 7 2 3 4 3 1 4 2 5 3 2 7 4 ...