This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'
using namespace std;
const int maxn = 1e6 + 3;
int n, m;
vector <pair <int, int> > adj[maxn];
void read()
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int a, b, s, t;
cin >> a >> b >> s >> t;
if (s == t)
continue;
adj[a].push_back({b, i});
adj[b].push_back({a, i});
}
}
int pos[maxn];
bool used[maxn];
void solve()
{
for (int i = 1; i <= n; i++)
if ((int)adj[i].size() % 2 == 1)
{
cout << "NIE" << endl;
return;
}
vector <vector <int> > ans;
for (int i = 1; i <= n; i++)
{
if (pos[i] == (int)adj[i].size())
continue;
vector <int> order;
stack <int> st;
st.push(i);
while (!st.empty())
{
int ver = st.top();
while (pos[ver] < (int)adj[ver].size() && used[adj[ver][pos[ver]].second])
pos[ver]++;
if (pos[ver] == (int)adj[ver].size())
{
order.push_back(ver);
st.pop();
continue;
}
used[adj[ver][pos[ver]].second] = true;
st.push(adj[ver][pos[ver]].first);
}
for (auto i: order)
used[i] = false;
for (auto i: order)
{
if (st.empty() || !used[i])
{
st.push(i);
used[i] = true;
continue;
}
else
{
vector <int> cycle;
while (st.top() != i)
{
cycle.push_back(st.top());
used[st.top()] = false;
st.pop();
}
cycle.push_back(i);
ans.push_back(cycle);
}
}
}
cout << (int)ans.size() << endl;
for (auto i: ans)
{
cout << (int)i.size() << " ";
for (auto j: i)
cout << j << " ";
cout << i[0] << endl;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |