solution
stringlengths 11
983k
| difficulty
int64 0
21
| language
stringclasses 2
values |
---|---|---|
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
// #include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int maxn = 5e5+5;
const int mod = 1e9+7;
int a[maxn],b[maxn][2];
int map[maxn],s[maxn];
int find(int x){
if(map[x] == x) return x;
return map[x] = find(map[x]);
}
void insert(int x,int y){
int xx = find(x);
int yy = find(y);
map[xx] = map[yy];
}
int main(){
std::ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
// freopen("../in.txt","r",stdin); freopen("../out.txt","w",stdout);
int n,m;
int S = 0,ans = 1;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
for(int j=0;j<a[i];j++) cin>>b[i][j];
}
for(int i=1;i<=m;i++) map[i] = i;
int last = 0;
for(int i=1;i<=n;i++){
if(a[i] == 1){
if(last && find(last) == find(b[i][0])) continue;
else if(last) insert(last,b[i][0]);
last = b[i][0];
ans *= 2; ans %= mod;
s[S++] = i;
}else if(find(b[i][0]) == find(b[i][1])){
continue;
}else{
insert(b[i][0],b[i][1]);
ans *= 2; ans %= mod;
s[S++] = i;
}
}
cout<<ans<<" "<<S<<"\n";
sort(s,s+S);
for(int i=0;i<S;i++){
cout<<s[i]<<" ";
}
cout<<"\n";
return 0;
}
| 12 |
CPP
|
#include<bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// using namespace __gnu_pbds;
#pragma GCC optimize("O3")
#ifdef LOCAL
#include "/Users/lbjlc/Desktop/coding/debug_utils.h"
#else
#define print(...) ;
#define printn(...) ;
#define printg(...) ;
#define fprint(...) ;
#define fprintn(...) ;
#endif
#define rep(i, a, b) for(auto i = (a); i < (b); i++)
#define rrep(i, a, b) for(auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
// #define mp make_pair
#define fi first
#define se second
#define maxi(x, y) x = max(x, y)
#define mini(x, y) x = min(x, y)
// long long fact(long long n) { if(!n) return 1; return n*fact(n-1); }
// #define endl '\n'
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int get_random() {
static uniform_int_distribution<int> dist(0, 1e9 + 6);
return dist(rng);
}
#define solve_testcase int T;cin>>T;for(int t=1;t<=T;t++){solve(t);}
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pdd> vpdd;
typedef vector<long long> vll;
#define bd(type,op,val) bind(op<type>(), std::placeholders::_1, val)
template<class T>
void make_unique(T & v) {
sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());
}
int geti() { int x; cin >> x; return x; }
long long getll() { long long x; cin >> x; return x; }
double getd() { double x; cin >> x; return x; }
// pair<int, int> a(geti(), geti()) does not work
// pair<int, int> a({geti(), geti()}) works, since it uses initializer.
const int MAXN = 5e5 + 100;
struct disjoint_set_union{
int root[MAXN], size[MAXN];
int l, r;
void init(int _l, int _r) {
l = _l; r = _r;
for(int i = l; i <= r; i++) {
root[i] = i;
size[i] = 1;
}
}
int find(int i) {
if(root[i] != i)
return root[i] = find(root[i]);
else
return i;
}
int same(int i, int j) {
return find(i) == find(j);
}
void join(int i, int j) {
if(same(i, j))
return;
// print("join",i,j);
int ri = find(i);
int rj = find(j);
if(size[ri] >= size[rj]) {
root[rj] = ri;
size[ri] += size[rj];
}
else {
root[ri] = rj;
size[rj] += size[ri];
}
}
};
disjoint_set_union dsu;
int vis[MAXN]={};
ll mod=1e9+7;
ll mypow(ll x, ll n) {
if(!n) return 1;
ll res=mypow(x,n/2);
if(n%2) return res*res%mod*x%mod;
else return res*res%mod;
}
void solve(int tt) {
// cout<<"Case #"<<tt<<": ";
}
int main(int argc, char * argv[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// solve_testcase;
int n,m,k,x,y;
cin>>n>>m;
dsu.init(1,m);
vi res;
rep(i,0,n) {
cin>>k;
if(k==1) {
cin>>x;
int & tmp = vis[dsu.find(x)];
if(!tmp) {
tmp=1;
res.pb(i+1);
}
else if(tmp==2) {
tmp=1;
res.pb(i+1);
}
}
else {
cin>>x>>y;
if(dsu.same(x,y)) continue;
int t1 = vis[dsu.find(x)];
int t2 = vis[dsu.find(y)];
if(!t1&&!t2) {
dsu.join(x,y);
vis[dsu.find(x)]=2;
res.pb(i+1);
}
else if(t1&&!t2) {
int t=vis[dsu.find(x)];
dsu.join(x,y);
vis[dsu.find(x)]=t;
res.pb(i+1);
}
else if(!t1&&t2) {
int t=vis[dsu.find(y)];
dsu.join(x,y);
vis[dsu.find(y)]=t;
res.pb(i+1);
}
else if(t1==1&&t2==2) {
dsu.join(x,y);
vis[dsu.find(y)]=1;
res.pb(i+1);
}
else if(t1==2&&t2==1) {
dsu.join(x,y);
vis[dsu.find(x)]=1;
res.pb(i+1);
}
else if(t1==2&&t2==2) {
dsu.join(x,y);
// vis[dsu.find(x)]=1;
res.pb(i+1);
}
}
}
ll ans=1;
rep(i,1,m+1) {
if(dsu.find(i)==i && vis[i]) {
print(i,vis[i],dsu.size[i]);
if(vis[i]==2)
ans *= mypow(2,dsu.size[i]-1);
else
ans *= mypow(2,dsu.size[i]);
ans %= mod;
}
}
cout<<ans<<' '<<res.size()<<endl;
for(auto x:res)cout<<x<<' ';
cout<<endl;
return 0;
}
| 12 |
CPP
|
/*
`-:://:::-
`//:-------:/:`
.+:--.......--:+`
`+:--..`````..--//`
.o:--..`` ``..--:o`
.o:--...```..---+/`
`/y+o/---....---:+o.
`...````-os+/:---:/+o/--.`
`-/+++++/:. `...` :h+d+oooo+/+-` ...
`/++//:::://++-`....` -.`//````````:` `..`
`o+/::------://o/` `-` -. -` `..`
`---.-o/:./o/::-..``..-ЗАПУСКАЕМ .. .. -` `... ``..``
`....o+:-++/:--.```..-://s. `-` .- -` `-o: .-//::::/:-`
`:s+/:--....-::/+s-` .- `- -` -///:--------:/:`
./s+//:::::://oo-``..НЕЙРОННУЮ: СЕТЬ:::::::-`РАБОТЯГИ `+:--........--:/`
.:ooo+++++osso-` `.:-...`/` ./::-------:/:` -` :+--..``````.--:+:...-+:-`
`.-/+++++/+-.-` -. ``:so:/:--.......--:+` `-```````o+/+--..`````..--:o/-..:s+:.
```````:``.. `-` -` `+:--..`````..--/+-.../.`````..-o:--.......---/o. `
`: `:- -. .o:--..`` ``..--:o` `-` `:o+:--------:+o-`
`-`-... .. .o/--...```..--:+/` `-` `oy/so/////++o/.`
-/` `-` `- ``+s/o/:---...---:++. `-` .-../d://///:-.`
`.---..``-..- .-/..`````-oo+/:::::/+o+- `-``-` `-. ````
`:++++/+++++- ..``.-/:` /y-:/++o++/:.`..` ./. `-
-++/::::::://+/..:-``:` .. `-.` ```.``` `..` `..`-` `-
`` -o//:--....-::/++` -.-` `-`.-` `..`..` `-.-
-----ss+:++/:--.```..-://s. /. `:: `-:. ./`
`````/:..+o/::-..``.--:/+s. ..-` `-``-` ..` `-` `-`-`
`-s+/::-----::/+oo---``-` .. .:- ``` .-` .-.- `-`
`:oo+//::://+os/..:`..-/:` :y.-:::::::.`.-` ./-` `-`
`./+oooooooo+/.`- .-:...`.. .//:-------://` `- `..` `:.
``.-::::-.``-/` `-` `- `oo:+:--.......--:/` `- `.:--h.``..```
-.-`.- .- `+:--..`````..--//` `- /s-//::::::::.
-` `/- .. .o:--..`` ``..--:o.```.- `//:--------://`
-` .-`.-` -.`-o/--...```..--:+/.``-:....``:-.+:--....`...--:+`
..`-. `-. ``:os:o/:---...---:++. `- ``///+:-..``````.--:+-````-.`
`.:///////.-` .:-..` -``-+o+/:::::/+o/. `- `:+:-..`````..--:o/:--/ys+-
`-++///////+o/. ``....`-. :` `.:++++++/:.` .- -o/---......---/o. `.`
`++//:-----::/+o:..` .-` : ``````` .- `+so+:--------:++-`
`````:-``:o/::-..`..--:/+o` -. `- .- `../../+o+////+o+:.`
-----syo/o+/:--.```..-://s. .-` `- .- `... ``-:////:-``
.` `/s//:--....-::/+s. -. `-` .- `..`
.+o+/:::--:://+s/-..` .::+y ``` .- `..`
./oo++////+oso-` `.... :y-+:::::::/` ...
`.:+oooooo/-` `....-. .//:-------:/:-.`
``...`` /+:+:--.......--:+`
`+:--..`````..--//`
.o:--..`` ``..--:o`
.+/--...```..--:+/`
`-o/:---...---:++.
`-+o+/:---:/+o/.
`.:+oooo+/-.`
``````
*/
#ifdef aimbot
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#endif
#define hur(f, g) template<class c> int f(c a) {if (sizeof(c) == 8) return g##ll(a); else return g(a);}
hur(popc, __builtin_popcount) hur(ctz, __builtin_ctz) hur(clz, __builtin_clz)
/*
- place bitset modifications here
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <queue>
#include <ostream>
#include <istream>
#include <typeinfo>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <limits>
#include <fstream>
#include <array>
#include <list>
#include <bitset>
#include <functional>
#include <random>
#include <cstring>
#include <chrono>
#define random escape__from__random__aetuhoetnuhshe
#define mt make_tuple
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define le(v) ((int)v.size())
#define f(i, n) for (int i = 0; i < (n); i++)
#define rof(i, n) for (int i = ((n) - 1); i >= 0; i--)
#define apply(v, act) for (auto &x : v) { act; }
#define log(args...) {string s = #args;deque<string> deq;\
string buf = "";int bal = 0;for (char c : s) {\
if (c == '(' || c == '[' || c == '{') {bal++;\
} else if (c == ')' || c == ']' || c == '}') {\
bal--;} else {if (bal == 0) {if (c == ',') {\
deq.pb(buf);buf = "";} else {if (c != ' ') {\
buf += c;}}}}}if (!buf.empty()) {deq.pb(buf);}\
smart_io::precall_print();smart_io::_print(deq, args);}
inline int min(const int &x, const int &y) { return (((y-x)>>(32-1))&(x^y))^x; }
inline int max(const int &x, const int &y) { return (((y-x)>>(32-1))&(x^y))^y; }
inline long long min(const long long &x, const long long &y) { return (((y-x)>>(64-1))&(x^y))^x; }
inline long long max(const long long &x, const long long &y) { return (((y-x)>>(64-1))&(x^y))^y; }
#define print \
smart_io::precall_print(); \
cout,
#define scan cin,
#ifdef fast_allocator
const int MAXMEM = 200 * 1000 * 1024;
char _memory[MAXMEM];
size_t _ptr = 0;
void* operator new(size_t _x) { _ptr += _x; assert(_ptr < MAXMEM); return _memory + _ptr - _x; }
void operator delete (void*) noexcept {}
#endif
using namespace std;
char string_in_buffer[(int)260];
void fast_scan(int &x) { scanf("%d", &x); }
void fast_scan(long long &x) { scanf("%lld", &x); }
void fast_scan(unsigned long long &x) { scanf("%llu", &x); }
void fast_scan(double &x) { scanf("%lf", &x); }
void fast_scan(long double &x) { scanf("%Lf", &x); }
void fast_scan(char &x) {
scanf("%c", &x);
if (x == '\n') {
fast_scan(x);
}
}
void fast_scan(string &x) {
scanf("%s", string_in_buffer);
x = string(string_in_buffer);
}
template<class TFirst, class TSecond>
void fast_scan(pair<TFirst, TSecond> &p) {
fast_scan(p.first);
fast_scan(p.second);
}
template <class T>
void fast_scan(vector<T> &v) {
for (auto &x : v) fast_scan(x);
}
void fast_print(const int &x) { printf("%d", x); }
void fast_print(const unsigned int &x) { printf("%u", x); }
void fast_print(const long long &x) { printf("%lld", x); }
void fast_print(const unsigned long long &x) { printf("%llu", x); }
void fast_print(const char &x) { printf("%c", x); };
// void fast_print(__int128 x) {
// if (x == 0) { fast_print('0'); return; }
// if (x < 0) {
// fast_print('-');
// x = -x;
// }
// __int128 p = 1;
// while (x / (p * 10)) p *= 10;
// while (p) {
// __int128 symb = x / p;
// fast_print((int)symb);
// x -= p * symb;
// p /= 10;
// }
// };
void fast_print(const double &x) { printf("%.15lf", x); }
void fast_print(const long double &x) { printf("%.15Lf", x); }
void fast_print(const string &x) { printf("%s", x.c_str());}
void fast_print(const char v[]) { fast_print((string)v); }
template<class TFirst, class TSecond>
void fast_print(const pair<TFirst, TSecond> &p) {
fast_print(p.first);
fast_print(' ');
fast_print(p.second);
}
template <class T>
void fast_print(const vector<T> &v) {
if (v.empty()) return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print(' ');
fast_print(v[i]);
}
}
template <class T>
void fast_print(const vector<vector<T>> &v) {
if (v.empty()) return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print('\n');
fast_print(v[i]);
}
}
template <class T>
void fast_print(const T &v) {
for (const auto &x : v) {
fast_print(x);
fast_print(' ');
}
}
using namespace std;
namespace smart_io {
string print_start = "";
string sep = " ";
bool first_print = false;
void precall_print() {
fast_print(print_start);
print_start = "\n";
first_print = true;
}
void _print(deque<string>) {}
template<class T, class... Args>
void _print(deque<string> names, T elem, Args... args) {
if (!first_print) {
fast_print("\n");
} else {
first_print = false;
}
fast_print(names.front());
fast_print(" = ");
fast_print(elem);
names.pop_front();
_print(names, args...);
}
} //namespace smart_io
template <class T>
ostream &operator,(ostream &os, const T &object) {
if (!smart_io::first_print) {
fast_print(smart_io::sep);
} else {
smart_io::first_print = false;
}
fast_print(object);
return os;
}
template <class T>
istream &operator,(istream &is, T &object) {
fast_scan(object);
return is;
}
namespace random {
using namespace std::chrono;
mt19937 rng(duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
).count());
uniform_real_distribution<> prob_dist(0.0, 1.0);
};
namespace typedefs {
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef long double ld;
}
namespace numbers_operation {
template<class T>
inline T floor_mod(T a, const T &b) {
a %= b;
if (a < 0) a += b;
return a;
}
}
using namespace numbers_operation;
using namespace typedefs;
using namespace random;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
template<ll MOD>
struct Ring {
ll value = 0;
Ring() {}
Ring(int _value) {
value = _value;
value = floor_mod(value, MOD);
}
Ring(ll _value) {
value = _value;
value = floor_mod(value, MOD);
}
Ring pow(ll p) const {
Ring r = 1;
Ring x; x.value = value;
while (p) {
if (p & 1) r *= x;
x *= x;
p /= 2;
}
return r;
}
Ring inv() const {
return pow(MOD - 2);
}
void operator*=(const Ring<MOD> &b) {
value *= b.value;
value = floor_mod(value, MOD);
}
friend Ring operator*(Ring<MOD> a, const Ring<MOD> &b) {
a *= b;
return a;
}
void operator+=(const Ring<MOD> &b) {
value += b.value;
value -= (value >= MOD) * MOD;
}
friend Ring operator+(Ring a, const Ring &b) {
a += b;
return a;
}
void operator-=(const Ring<MOD> &b) {
value -= b.value;
value += (value < 0) ? MOD : 0;
}
friend Ring operator-(Ring a, const Ring &b) {
a -= b;
return a;
}
void operator/=(const Ring<MOD> &b) {
(*this) *= b.inv();
}
friend Ring operator/(Ring a, const Ring &b) {
a /= b;
return a;
}
bool operator==(const Ring<MOD> &b) {
return value == b.value;
}
bool operator!=(const Ring<MOD> &b) {
return value != b.value;
}
friend void fast_print(const Ring<MOD> &b) {
fast_print(b.value);
}
};
typedef Ring<MOD> num;
int n, m;
vector<int> up;
vector<bool> one;
int get_root(int v) {
if (up[v] == v) return v;
return up[v] = get_root(up[v]);
}
signed main(signed argc, char *argv[]) {
scan n, m;
up.resize(m);
one.resize(m);
iota(up.begin(), up.end(), 0);
vector<int> pick;
f(i, n) {
int k; scan k;
vector<int> cords(k);
scan cords;
apply(cords, x--)
if (k == 1) {
int top = get_root(cords[0]);
if (!one[top]) {
one[top] = true;
pick.pb(i + 1);
}
} else if (k == 2) {
int a = get_root(cords[0]);
int b = get_root(cords[1]);
if (a != b && !(one[a] && one[b])) {
if (one[b]) {
one[a] = true;
}
up[b] = a;
pick.pb(i + 1);
}
}
}
print num(2).pow(le(pick)), le(pick);
print pick;
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
#define debbuging false
#define _ if(!debbuging) ios_base::sync_with_stdio(0);cin.tie(0);
#define debug if(debbuging) cout
#define endl '\n'
typedef long long ll;
typedef tuple<int,int,int> t3;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const int MOD = 1e9 + 7;
struct dsu {
vector<int> id, sz;
dsu(int sz_) : id(sz_), sz(sz_,1) { iota(id.begin(), id.end(),0); }
int find(int a) { return id[a] = a == id[a] ? a : find(id[a]); }
void unite(int a, int b) {
a = find(a), b = find(b);
if(a == b) return;
if(sz[a] < sz[b]) swap(a,b);
sz[a] += sz[b];
id[b] = a;
}
};
int main(){ _
int n, m; cin >> n >> m;
vector<vector<int>> g(m+1);
vector<t3> edg;
for(int i = 0; i < n; i++) {
int k; cin >> k;
if(k == 1) {
int a; cin >> a; a--;
g[m].push_back(a);
g[a].push_back(m);
edg.push_back({i,a,m});
}
else {
int a, b; cin >> a >> b; a--, b--;
g[a].push_back(b);
g[b].push_back(a);
edg.push_back({i,a,b});
}
}
vector<int> vans;
sort(edg.begin(), edg.end());
dsu dsu(m+1);
for(auto [id,a,b] : edg) if(dsu.find(a) != dsu.find(b)) {
vans.push_back(id);
dsu.unite(a,b);
}
vector<ll> p2(m+1);
ll p = 1;
for(int i = 0; i <= m; i++) p2[i] = p, p = (p * 2) % MOD;
cout << p2[vans.size()] << ' ' << vans.size() << endl;
sort(vans.begin(), vans.end());
for(auto u : vans) cout << u + 1 << ' ';
cout << endl;
return 0;
}
| 12 |
CPP
|
MOD=10**9+7
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
def find(self, a): #return parent of a. a and b are in same set if they have same parent
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a: #path compression
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b): #union a and b
self.parent[self.find(b)] = self.find(a)
def oneLineArrayPrint(arr):
print(' '.join([str(x+1) for x in arr]))
def solveActual():
uf=UnionFind(m+2)
for i,x in enumerate(vS):
if len(x)==1:
x.append(m+1)
x.append(i) #vS' elements are now [p1,p2,index]
vS.sort(key=lambda x:x[2])
sPrime=[]
for p1,p2,index in vS:
if uf.find(p1)!=uf.find(p2): #won't form cycle
uf.union(p1,p2)
sPrime.append(index)
TSize=pow(2,len(sPrime),MOD)
print('{} {}'.format(TSize,len(sPrime)))
oneLineArrayPrint(sPrime)
n,m=[int(x) for x in input().split()]
vS=[] #0-indexed
for _ in range(n):
xx=[int(x) for x in input().split()]
#number of 1s, coordinates with 1s
for i in range(1,len(xx)):
xx[i]-=1 #0-index
vS.append(xx[1:])
solveActual()
| 12 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
using dbl = double;
template<typename T1, typename T2> bool chkmin(T1 &x, T2 y) { return y < x ? (x = y, true) : false; }
template<typename T1, typename T2> bool chkmax(T1 &x, T2 y) { return y > x ? (x = y, true) : false; }
void debug_out()
{
cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B)
{
cerr << ' ' << A;
debug_out(B...);
}
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 1337
#endif
const int maxN = 500105;
const int MOD = 1e9 + 7;
int add(int a, int b)
{
a += b;
if (a >= MOD)
a -= MOD;
return a;
}
void vadd(int &a, int b)
{
a += b;
if (a >= MOD)
a -= MOD;
}
int mult(int a, int b)
{
return a * (ll)b % MOD;
}
int p2[maxN];
int q[maxN];
int badc[maxN];
int gt(int x)
{
return q[x] < 0 ? x : q[x] = gt(q[x]);
}
bool un(int a, int b)
{
a = gt(a);
b = gt(b);
if (a == b)
return false;
if (-q[a] > -q[b])
swap(a, b);
q[b] += q[a];
badc[b] += badc[a];
q[a] = b;
return true;
}
signed main()
{
#ifdef DEBUG
freopen("in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
memset(q, 255, sizeof q);
p2[0] = 1;
for (int i = 1; i < maxN; ++i)
p2[i] = mult(2, p2[i - 1]);
int n, m;
cin >> n >> m;
vector<int> ans;
for (int i = 1; i <= n; ++i)
{
int k;
cin >> k;
if (k == 1)
{
int a;
cin >> a;
a = gt(a);
if (badc[a] == 0)
{
ans.push_back(i);
++badc[a];
}
}
else
{
int a, b;
cin >> a >> b;
a = gt(a);
b = gt(b);
if (a == b || badc[a] + badc[b] > 1)
continue;
if (-q[a] > -q[b])
swap(a, b);
q[b] += q[a];
badc[b] += badc[a];
q[a] = b;
ans.push_back(i);
}
}
cout << p2[ans.size()] << ' ' << ans.size() << '\n';
for (auto i : ans)
cout << i << ' ';
cout << '\n';
return 0;
}
| 12 |
CPP
|
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
def find(self, a):
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a:
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b):
self.parent[self.find(b)] = self.find(a)
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
UF = UnionFind(m + 1)
MOD = 10 ** 9 + 7
out = []
for i in range(1, n + 1):
l = list(map(int, input().split()))
if len(l) == 2:
u = 0
v = l[1]
else:
_, u, v = l
uu = UF.find(u)
vv = UF.find(v)
if uu != vv:
UF.union(uu,vv)
out.append(i)
print(pow(2, len(out), MOD), len(out))
print(' '.join(map(str,out)))
| 12 |
PYTHON3
|
import sys
input = sys.stdin.readline
mod=1000000007
n,m=map(int,input().split())
ans=[]
groupi=[-1]*(m+1)
groups=[2]*m
for i in range(m):
groups[i]=[]
cur=1
for i in range(n):
x=list(map(int,input().split()))
k=x.pop(0)
if k==1:
x=x[0]
if groupi[x]==-1:
groupi[x]=0
ans.append(i+1)
if groupi[x]>0:
ind=groupi[x]
for y in groups[ind]:
groupi[y]=0
groupi[x]=0
ans.append(i+1)
if k==2:
x1,x2=x[0],x[1]
if groupi[x1]==-1:
if groupi[x2]==-1:
groupi[x1]=cur
groupi[x2]=cur
groups[cur]=[x1,x2]
cur+=1
ans.append(i+1)
else:
if groupi[x2]==0:
groupi[x1]=0
ans.append(i+1)
else:
groupi[x1]=groupi[x2]
groups[groupi[x2]].append(x1)
ans.append(i+1)
else:
if groupi[x2]==-1:
if groupi[x1]==0:
groupi[x2]=0
ans.append(i+1)
else:
groupi[x2]=groupi[x1]
groups[groupi[x1]].append(x2)
ans.append(i+1)
else:
if groupi[x1]!=groupi[x2]:
if groupi[x1]==0 or groupi[x2]==0:
if groupi[x1]==0:
for y in groups[groupi[x2]]:
groupi[y]=0
else:
for y in groups[groupi[x1]]:
groupi[y]=0
else:
if len(groups[groupi[x1]])<len(groups[groupi[x2]]):
x1,x2=x2,x1
for y in groups[groupi[x2]]:
groupi[y]=groupi[x1]
groups[groupi[x1]].append(y)
ans.append(i+1)
print(pow(2,len(ans),mod),len(ans))
print(*ans)
| 12 |
PYTHON3
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> G;
int get_group(int x) {
if(G[x] == x)
return x;
return G[x] = get_group(G[x]);
}
void merge_group(int x, int y) {
int gx = get_group(x);
int gy = get_group(y);
if(gx == gy) return;
if(gx > gy)
swap(gx, gy);
G[gx] = G[gy];
}
constexpr long long MOD = 1e9 + 7;
void solve(int TestCase) {
int n, m;
cin >> n >> m;
int k = max(n, m);
vector<vector<int>> A(n, vector<int>(4));
for(auto i = 0; i < n; ++i) {
int cnt;
cin >> cnt;
for(auto j = 0; j < cnt; ++j)
cin >> A[i][j];
if(cnt == 1) A[i][1] = k+1;
A[i][2] = i + 1;
if(cnt == 2 && A[i][0] > A[i][1])
swap(A[i][0], A[i][1]);
}
G.resize(k+3);
iota(G.begin(), G.end(), 0);
for(auto& a : A) {
auto g0 = get_group(a[0]);
auto g1 = get_group(a[1]);
if(g0 == g1) a[3] = 1;
else merge_group(a[0], a[1]);
}
int cnt = 0;
for(auto& a : A)
if(!a[3]) cnt++;
ll ret = 1;
for(auto i = 0; i < cnt; ++i)
ret = ret * 2 % MOD;
cout << ret << " " << cnt << endl;
for(auto& a : A)
if(!a[3]) cout << a[2] << " ";
cout << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
//cin >> t;
for(auto i = 1; i <= t; ++i) {
//cout << "Case #"<< i << ": ";
solve(i);
}
}
| 12 |
CPP
|
import sys
def input():
return sys.stdin.buffer.readline()[:-1]
class UnionFind():
def __init__(self, size):
self.table = [-1 for _ in range(size)]
def find(self, x):
while self.table[x] >= 0:
if self.table[self.table[x]] >= 0:
self.table[x] = self.table[self.table[x]]
x = self.table[x]
return x
def same(self, x, y):
return self.find(x) == self.find(y)
def unite(self, x, y):
s1 = self.find(x)
s2 = self.find(y)
if s1 != s2:
r1 = self.table[s1]
r2 = self.table[s2]
if r1 <= r2:
self.table[s2] = s1
if r1 == r2:
self.table[s1] -= 1
else:
self.table[s1] = s2
return
m, n = map(int, input().split())
uf = UnionFind(n+1)
ans = []
for i in range(1, m+1):
q = list(map(int, input().split()))
if q[0] == 1:
if uf.same(q[1]-1, n):
continue
else:
ans.append(i)
uf.unite(q[1]-1, n)
else:
if uf.same(q[1]-1, q[2]-1):
continue
else:
ans.append(i)
uf.unite(q[1]-1, q[2]-1)
print(pow(2, len(ans), 10**9+7), len(ans))
print(*ans)
| 12 |
PYTHON3
|
#include<bits/stdc++.h>
#define int long long
#define mod 1000000007
using namespace std;
int freex[500005];
int parent[500005];
vector<int> adj[500005];
int finder(int x)
{if(x==parent[x]){return x;}
int z=finder(parent[x]);
parent[x]=z;
return z;
}
void Union(int a,int b)
{if(finder(a)==finder(b)){return;}
int x=finder(a);
int y=finder(b);
parent[x]=y;
}
int32_t main()
{ios::sync_with_stdio(0);
cin.tie(0);
memset(freex,0,sizeof(freex));
int n,mi;
cin>>n>>mi;
int k;
vector<int> v;
for(int i=0;i<500005;i++){parent[i]=i;}
//Remember to go from 1 to m
int x,y;
for(int i=0;i<n;i++)
{cin>>k;
if(k==1)
{cin>>x;y=500004;
int z=finder(x);
}
else
{//int x,y;
cin>>x>>y;}
//cout<<finder(x)<<" "<<finder(y)<<"\n";
if(finder(x)==finder(y)){continue;}
v.push_back(i+1);
freex[x]++;freex[y]++;
adj[x].push_back(y);
adj[y].push_back(x);
Union(x,y);
}
int ans=1;
map<int,int> m;
for(int i=0;i<500005;i++)
{if(freex[i]==0){continue;}
m[finder(i)]++;
}
for(auto x:m)
{//cout<<x.second<<"\n";
for(int y=0;y<x.second-1;y++){ans=ans*2;ans%=mod;}
}
cout<<ans<<" ";
cout<<v.size()<<"\n";
for(auto x:v){cout<<x<<" ";}
}
| 12 |
CPP
|
#include "bits/stdc++.h"
#define requires(...) typename std::enable_if<__VA_ARGS__::value, int>::type = 0
using namespace std;
template<class U, class V>
istream &operator>>(istream &is, pair<U, V> &p) { return is >> p.first >> p.second; }
template<class U, class V>
ostream &operator<<(ostream &os, const pair<U, V> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template<class Istream, class Container, requires(is_same<Istream, istream>)>
Istream &operator>>(Istream &is, Container &container) {
for (auto &value : container) is >> value;
return is;
}
template<class Ostream, class Container, requires(is_same<Ostream, ostream>)>
Ostream &operator<<(Ostream &os, const Container &container) {
auto _begin = begin(container), _end = end(container);
for (auto it = _begin; it != _end;)
os << "{ "[it != _begin] << *it << ",}"[++it == _end];
return os;
}
namespace io {
template<class ...As>
struct last {
};
template<class ...As> using last_t = typename last<As...>::type;
template<class A>
struct last<A> {
using type = A;
};
template<class A, class ...As>
struct last<A, As...> {
using type = typename last<As...>::type;
};
template<class Z>
Z read(Z &) {
Z z;
cin >> z;
return z;
}
template<class A, class ...As>
last_t<As...> read(A &a, As &...as) { return cin >> a, read(as...); }
void log_rest() {}
template<class A, class ...As>
void log_rest(const A &a, const As &...as) {
cerr << ", " << a;
log_rest(as...);
}
template<class A, class ...As>
void log(const string &pref, const A &a, const As &...as) { cerr << pref << a, log_rest(as...); }
} // namespace io
#define A(xs) begin(xs), end(xs)
#define B(...) [&](auto &&lhs, auto &&rhs) { \
return __VA_ARGS__; \
}
#define U(...) [&](auto &&lhs, auto &&rhs) { \
auto predicate = [&](auto &&x) { \
return __VA_ARGS__; \
}; \
return predicate(lhs) < predicate(rhs); \
}
#define X first
#define Y second
#define PB push_back
#define EB emplace_back
#define R(...) __VA_ARGS__ = io::read(__VA_ARGS__)
#define RC(name, ...) name(__VA_ARGS__); cin >> name
#define G3(_1, _2, _3, FUNC, ...) FUNC
#define F1(i, n) for (decltype(n) i = {}; i != n; ++i)
#define F2(i, a, b) for (typename common_type<decltype(a), decltype(b)>::type \
down = a > b, i = a - down; i + down != b; \
down ? --i : ++i)
#define F(...) G3(__VA_ARGS__, F2, F1)(__VA_ARGS__)
#ifdef DEBUG
int recursion_depth = 0;
# define D for (bool _flag = true; _flag; _flag = !_flag)
# define L(...) (++recursion_depth, \
io::log(string(recursion_depth - 1, '\t') + \
string(__func__) + ":" + to_string(__LINE__) + \
" \t( "#__VA_ARGS__" ) := ", \
__VA_ARGS__), \
--recursion_depth, cerr << "\n")
# define dbg(...) [&](const string &func) -> auto && { \
++recursion_depth; \
auto&& value = __VA_ARGS__; \
--recursion_depth; \
cerr << string(recursion_depth, '\t') \
<< func << ":" << __LINE__ \
<< " \t"#__VA_ARGS__" = " << value << endl; \
return forward<decltype(value)>(value); \
}(__func__)
#else
# define L(...) while (false) cerr
# define D while (false)
# define dbg(...) (__VA_ARGS__)
#endif
template<class T>
T make_vec(T default_value) { return default_value; }
template<class T, class Arg, class ...Args>
auto make_vec(T default_value, Arg size, Args ...rest)
-> vector<decltype(make_vec(default_value, rest...))> {
auto level = make_vec(default_value, rest...);
return vector<decltype(level)>(size, level);
}
template<class Xs>
int sz(const Xs &xs) { return static_cast<int>(xs.size()); }
using i64 = int64_t;
using f80 = long double;
using Str = string;
template<class T = int> using Vec = vector<T>;
template<class K = int, class H = hash<K>> using US = unordered_set<K, H>;
template<class K, class V, class H = hash<K>> using UM = unordered_map<K, V, H>;
template<class U = int, class V = U> using P = pair<U, V>;
using G = Vec<Vec<int>>;
template<class T, class P>
auto bin_search(T l, T r, P p) -> T {
for (T m; m = (l + r) / 2, m != l && m != r; (p(m) ? l : r) = m);
return l;
}
Vec<int> parents, sze;
Vec<int> bad;
int get_root(int v) {
if (parents[v] == v)
return v;
return parents[v] = get_root(parents[v]);
}
bool connected(int u, int v) {
return get_root(u) == get_root(v);
}
void unite(int u, int v) {
int a = get_root(u);
int b = get_root(v);
if (sze[a] < sze[b]) swap(a, b);
parents[b] = a;
sze[a] += sze[b];
bad[a] += bad[b];
}
int64_t mod = 1000000007;
int main() {
int R(n, m);
parents.assign(m, 0);
iota(A(parents), 0);
sze.assign(m, 1);
bad.assign(m, 0);
Vec<int> ans;
for (int i = 0; i < n; ++i) {
int R(k);
if (!k) continue;
if (k == 1) {
int R(x);
--x;
int root = get_root(x);
if (bad[root])
continue;
bad[root]++;
ans.push_back(i);
} else {
int R(x, y);
--x;--y;
int ar = get_root(x), br = get_root(y);
if (ar == br || (bad[ar] + bad[br] > 1))
continue;
unite(ar, br);
ans.push_back(i);
}
}
i64 answer = 1;
for (int i = 0; i < ans.size(); ++i) {
answer *= 2;
answer %= mod;
}
cout << answer << ' ' << ans.size() << '\n';
for (int x: ans) cout << x+ 1 << ' ';
return 0;
}
namespace {
auto fast_io = [] {
#ifndef DEBUG
# ifndef INTERACTIVE
ios::sync_with_stdio(false);
cin.tie(nullptr);
# endif // INTERACTIVE
# ifdef FILES
freopen(FILES".in", "r", stdin);
freopen(FILES".out", "w", stdout);
# endif // FILES
#endif // DEBUG
cout << setprecision(8) << fixed;
cerr << boolalpha << setprecision(4) << fixed;
return 0;
}();
} // namespace
| 12 |
CPP
|
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define allv(V) (V).begin(), (V).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = INT_MAX;
const ll infll = LONG_LONG_MAX;
const ll mod = 1000000007LL;
int gcd(int x, int y){return y ? gcd(y, x % y) : y;}
ll n, m, f[505050], par[505050];
vector<ll> v[505050], ansv;
void dfs(ll x)
{
f[x] = 1;
for(auto &i : v[x])
{
if(!f[i]) dfs(i);
}
}
ll p(ll x)
{
if(x == par[x]) return x;
return par[x] = p(par[x]);
}
void mer(ll x, ll y)
{
x = p(x);
y = p(y);
if(x != y) par[x] = y;
}
int main()
{
scanf("%lld %lld", &n, &m);
for(int i = 1; i <= m; i++) par[i] = i;
for(int i = 1; i <= n; i++)
{
ll x;
scanf("%lld", &x);
if(x == 1)
{
ll y;
scanf("%lld", &y);
if(f[y]) continue;
f[y] = 1;
ansv.pb(i);
dfs(y);
}
if(x == 2)
{
ll y, z;
scanf("%lld %lld", &y, &z);
if(f[y] && f[z]) continue;
if(p(y) == p(z)) continue;
if(f[y])
{
f[z] = 1;
ansv.pb(i);
dfs(z);
continue;
}
if(f[z])
{
f[y] = 1;
ansv.pb(i);
dfs(y);
continue;
}
mer(y, z);
v[y].pb(z);
v[z].pb(y);
ansv.pb(i);
}
}
ll ans = 1;
for(int i = 0; i < ansv.size(); i++) ans = ans * 2 % mod;
printf("%lld %lld\n", ans, ansv.size());
for(auto &i : ansv) printf("%lld ", i);
return 0;
}
| 12 |
CPP
|
import sys
input = sys.stdin.buffer.readline
def prog():
n,m = map(int,input().split())
mod = 10**9 + 7
has_one = [0]*(m+1)
basis = []
sizes = [1]*(m+1)
parent = list(range(m+1))
def find_parent(v):
if v == parent[v]:
return v
v = find_parent(parent[v])
return v
def union_sets(a,b):
a = find_parent(a)
b = find_parent(b)
if a != b and (not has_one[a] or not has_one[b]):
if sizes[a] < sizes[b]:
a,b = b,a
parent[b] = a
sizes[a] += sizes[b]
has_one[a] = has_one[a] | has_one[b]
return True
else:
return False
for i in range(1,n+1):
a = list(map(int,input().split()))
if a[0] == 1:
par = find_parent(a[1])
if not has_one[par]:
has_one[par] = 1
basis.append(i)
elif union_sets(a[1],a[2]):
basis.append(i)
basis.sort()
print(pow(2,len(basis),mod),len(basis))
print(*basis)
prog()
| 12 |
PYTHON3
|
#include <iostream>
#include <map>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <iomanip>
#include <bitset>
using namespace std;
#define pi pair<int ,int>
int mod = 1e9+7 , n , m , ok[1000000] , lab[1000000];
vector <int> ruler[1000001] , res;
void unite(int u , int v)
{
if (lab[u] > lab[v])
{
swap(u , v);
}
lab[u] += lab[v];
lab[v] = u;
}
int get(int u)
{
while (lab[u] > 0) u = lab[u];
return u;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> n >> m;
for (int i = 1 ; i <= n ; i++)
{
int k , u ,v;
cin >> k;
for (int e = 1 ; e <= k ; e++)
{
cin >> v;
ruler[i].push_back(v);
}
}
for (int i = 1 ; i <= m ; i++)
{
lab[i] = -1;
}
int pre = 0;
/*for (int i = 1 ; i <= n ; i++)
{
if (ruler[i].size() == 1)
{
if (pre != 0)
{
int u = ruler[i][0];
if (u != get(pre))
{
res.push_back(i);
unite(u , get(pre));
}
}
else
{
res.push_back(i);
}
pre = ruler[i][0];
}
}*/
for (int i = 1 ; i <= n ; i++)
{
if (ruler[i].size() == 1)
{
if (pre != 0)
{
int u = ruler[i][0];
if (get(u) != get(pre))
{
res.push_back(i);
unite(get(u) , get(pre));
}
}
else
{
res.push_back(i);
}
pre = ruler[i][0];
}
else
{
int u = ruler[i][0] , v = ruler[i][1];
int dadu = get(u) , dadv = get(v);
if (dadu != dadv && ok[u] + ok[v] != 2)
{
res.push_back(i);
unite(dadu , dadv);
}
}
}
int got = 1;
for (int i = 1 ; i <= res.size() ; i++)
{
got = 1ll * got * 2 % mod;
}
sort(res.begin() , res.end());
cout <<got<<" "<<res.size()<<'\n';
for (int i = 0 ; i < res.size() ; i++)
{
cout <<res[i]<<'\n';
}
}
| 12 |
CPP
|
import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
class DSU():
def __init__(self, n):
self.parent = list(range(n))
self.size = [1] * n
self.has_loop = [False] * n
def find(self, v):
if v == self.parent[v]:
return v
self.parent[v] = self.find(self.parent[v])
return self.parent[v]
def union(self, a, b):
a = self.find(a)
b = self.find(b)
if a != b:
if self.size[a] < self.size[b]:
a, b = b, a
self.parent[b] = a
self.size[a] += self.size[b]
self.has_loop[b] = self.has_loop[a] = (self.has_loop[a] or self.has_loop[b])
def solve():
n, m = map(int, input().split())
dsu = DSU(m)
ans = []
for i in range(n):
k, *r = map(int, input().split())
if k == 1:
x = r[0] - 1
if not dsu.has_loop[dsu.find(x)]:
dsu.has_loop[dsu.find(x)] = True
ans.append(i+1)
else:
u, v = r[0]-1, r[1]-1
if dsu.find(u) != dsu.find(v) and not (dsu.has_loop[dsu.find(u)] and dsu.has_loop[dsu.find(v)]):
dsu.union(u, v)
ans.append(i+1)
print(pow(2, len(ans), 10**9+7), len(ans))
print(*ans)
t = 1
for _ in range(t):
solve()
| 12 |
PYTHON3
|
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define lun(x) (int)x.size()
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<60);
const int inf=(1<<30);
const int nmax=5e5+50;
const ll mod=1e9+7;
using namespace std;
int n,m,i,j,c,vz[nmax],sz[nmax],p[nmax],k,x,y;
ll rs=1;
vector<int>g[nmax],v;
int fnd(int x)
{
if(p[x]==x)return x;
return p[x]=fnd(p[x]);
}
int uni(int x,int y)
{
x=fnd(x),y=fnd(y);
if(x==y)return 0;
if(sz[x]<sz[y])swap(x,y);
sz[x]+=sz[y];
p[y]=x;
return 1;
}
void dfs(int x)
{
vz[x]=1;
c++;
for(int i=0;i<lun(g[x]);i++)if(!vz[g[x][i]])dfs(g[x][i]);
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n>>m;
for(i=1;i<=m+1;i++)
{
sz[i]=1;
p[i]=i;
}
for(i=1;i<=n;i++)
{
cin>>k;
if(k==1)
{
cin>>x;
if(uni(x,m+1))
{
v.pb(i);
g[x].pb(m+1);
g[m+1].pb(x);
}
}
else
{
cin>>x>>y;
if(uni(x,y))
{
v.pb(i);
g[x].pb(y);
g[y].pb(x);
}
}
}
for(i=1;i<=m+1;i++)
{
if(vz[i])continue;
c=0;
dfs(i);
for(j=1;j<c;j++)rs=(rs*2LL)%mod;
}
cout<<rs<<" "<<lun(v)<<'\n';
for(i=0;i<lun(v);i++)cout<<v[i]<<" ";
cout<<'\n';
return 0;
}
| 12 |
CPP
|
//goto line 42 for some useful code
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.hpp"
#else
#define dbg(...) 47
#endif
// speed up hacks does not work with MSVC compilers
// refer https://codeforces.com/blog/entry/66279
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
// ..........................
typedef long long ll;
typedef unsigned long int ul;
typedef unsigned long long int ull;
typedef unsigned int ui;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> matrix;
typedef vector<ll> vll;
#define f(i, x, n) for (int i = x; i < n; i++)
#define rf(i, n, x) for(int i=n;i>=x;--i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define mem(a, b) memset(a, b, sizeof(a))
#define fast_io() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
const ll mod = 1e9+7;
const ll oo = INT64_MAX;
const int ran = 2e5+5;
//* for other functions and declarations .......
vector<int>parent, a;
vector<int> ans;
int root(int x){
while(a[x] != x){
a[x] = a[a[x]];
x = a[x];
}
return x;
}
void uni(int x, int y){
int p = root(x);
int q = root(y);
if(p<q) a[q] = p;
else a[p] = q;
}
void solve(){
int n,m;
cin>>n>>m;
a.resize(m+1);
for(int i=0;i<m+1;++i) a[i] = i;
vector<vector<int>> inp(n);
ll cnt = 1;
f(i, 1, n+1){
int k;
cin>>k;
int x, y=0;
cin>>x;
if(k ==2)cin>>y;
int rx = root(x);
int ry = root(y);
if(rx == ry)continue;
ans.pb(i);
uni(x, y);
cnt<<=1;
cnt%=mod;
}
cout<<cnt<<" "<<ans.size()<<endl;
for(auto &aa:ans)cout<<aa<<" ";
cout<<endl;
}
int main()
{
fast_io();
cerr << "...............Console is yours! :)................." << endl;
solve();
cerr<<"......^_^....."<<endl;
return 0;
}
| 12 |
CPP
|
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define em emplace
#define eb emplace_back
#define pb pop_back
#define sz(v) (int) v.size()
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int N = 5e5 + 5, MOD = 1e9 + 7;
int mul(int x, int y) {
return 1ll * x * y % MOD;
}
int n, m;
int p[N];
int get(int x) {
return x == p[x] ? x : p[x] = get(p[x]);
}
bool unite(int x, int y) {
x = get(x);
y = get(y);
if (x == y) {
return false;
}
p[y] = x;
return true;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i <= m; ++i) {
p[i] = i;
}
int ans = 1;
vector<int> answ;
for (int i = 1; i <= n; ++i) {
int k, x, y = 0;
cin >> k >> x;
if (k == 2) {
cin >> y;
}
if (unite(x, y)) {
ans = mul(ans, 2);
answ.eb(i);
}
}
cout << ans << " " << sz(answ) << "\n";
sort(all(answ));
for (int x : answ) {
cout << x << " ";
}
cout << "\n";
return 0;
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define pii pair<int, int>
#define pb emplace_back
#define pf emplace_front
#define mp make_pair
#define ld long double
#define all(x) x.begin(), x.end()
#define uniq(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
const int maxn = 5e5 + 9;
int p[maxn];
int sz[maxn];
bool kek[maxn];
int n, m;
int mod = 1e9 + 7;
int binpow(int x, int pw) {
if (pw == 0)
return 1;
if (pw == 1)
return x % mod;
int y = binpow(x, pw / 2);
y = (y * y) % mod;
if (pw % 2)
y = (y * x) % mod;
return y;
}
int find_p(int i) {
if (p[i] != i)
p[i] = find_p(p[i]);
return p[i];
}
void merg(int i, int e) {
i = find_p(i);
e = find_p(e);
if (i == e)
return;
if (sz[i] < sz[e])
swap(i, e);
sz[i] += sz[e];
p[e] = i;
kek[i] |= kek[e];
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
p[i] = i;
sz[i]= 1;
}
vector<int> ans;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
if (k == 1) {
int x;
cin >> x;
x--;
int v = find_p(x);
if (!kek[v]) {
kek[v] = 1;
ans.pb(i);
}
} else {
int x, y;
cin >> x >> y;
x--, y--;
int px = find_p(x);
int py = find_p(y);
if (px != py && (!kek[px] || !kek[py])) {
merg(x, y);
ans.pb(i);
}
}
}
cout << binpow(2, ans.size()) << " ";
cout << ans.size() << "\n";
for (int i : ans)
cout << i + 1 << " ";
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
class union_find{
public:
vector<int> arr;
union_find(int n){
arr.resize(n);
fill(arr.begin(),arr.end(),-1);
}
int find(int x){
return arr[x]<0?x:arr[x]=find(arr[x]);
}
void unite(int x,int y){
x=find(x),y=find(y);
if(x==y) return;
if(arr[x]>arr[y]) swap(x,y);
arr[x]+=arr[y];
arr[y]=x;
}
bool connected(int x,int y){
return find(x) == find(y);
}
int size(int x){
return -arr[find(x)];
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin >> n >> m;
vector<bool> ta(m);
int kos=0;
union_find uf(m);
vector<vector<int>> ad(m);
function<void(int,int)> dfs=[&](int cur,int par){
ta[cur]=true;
for(int ch:ad[cur]){
if(ch==par) continue;
if(ta[ch]) continue;
ta[ch]=true;
dfs(ch,cur);
}
};
vector<int> res;
for(int i=0;i<n;i++){
int k;
cin >> k;
if(k==1){
int a;
cin >> a;
a--;
if(ta[a]) continue;
ta[a]=true;
dfs(a,-1);
res.push_back(i);
}else{
int a,b;
cin >> a >> b;
a--,b--;
if(uf.connected(a,b)) continue;
uf.unite(a,b);
ad[a].push_back(b);
ad[b].push_back(a);
if(ta[a]){
if(ta[b]) continue;
res.push_back(i);
dfs(b,-1);
}else{
res.push_back(i);
if(ta[b]) dfs(a,-1);
}
}
}
long long c=1;
long long mod=1000000007;
for(int i=0;i<res.size();i++) c=c*2%mod;
cout << c << " " << res.size() << "\n";
for(int i=0;i<res.size();i++){
cout << res[i]+1 << " ";
}
cout << "\n";
return 0;
}
| 12 |
CPP
|
import sys,io,os
from collections import deque
try:Z=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
except:Z=lambda:sys.stdin.readline().encode()
Y=lambda:map(int,Z().split())
M=10**9+7
n,N=Y()
def path(R):
H=deque();H.append(R)
while P[R]>=0:
R=P[R];H.append(R)
if len(H)>2:P[H.popleft()]=H[-1]
return R
K=[-1]*N;P=[-1]*N;S=[1]*N;R=0;B=[];alr=[0]*N
for i in range(n):
k=*Y(),
if k[0]<2:
a=k[1]-1
if K[a]>=0:
v=path(K[a])
if not alr[v]:alr[v]=1
else:continue
else:K[a]=R;v=R;alr[R]=1;R+=1
B.append(i+1)
continue
a=k[1]-1;b=k[2]-1
if K[a]>=0:
if K[b]>=0:
va,vb=path(K[a]),path(K[b])
if va==vb or (alr[va] and alr[vb]):pass
else:
sa,sb=S[va],S[vb]
if sa>sb:P[vb]=va
else:
P[va]=vb
if sa==sb:S[vb]+=1
B.append(i+1)
if alr[va]:alr[vb]=1
if alr[vb]:alr[va]=1
else:K[b]=path(K[a]);B.append(i+1)
else:
if K[b]>=0:vb=K[a]=path(K[b]);B.append(i+1)
else:K[a]=R;K[b]=R;R+=1;B.append(i+1)
B.sort()
s=len(B)
print(pow(2,s,M),s)
print(' '.join(map(str,B)))
| 12 |
PYTHON3
|
from sys import stdin,stdout
n,m=[int(i) for i in stdin.readline().split()]
l=[]
d1={}
a=[]
p=[i for i in range(m)]
r=[0 for i in range(m)]
mod=10**9+7
def find(x):
while p[x]!=x:
x=p[x]
return x
def power(x):
f=1
ct=2
while x:
if x%2==1:
f=f*ct
f%=mod
ct=ct*ct
ct%=mod
x//=2
return f
for i in range(n):
x=[int(j)-1 for j in stdin.readline().split()][1:]
if len(x)==1:
x[0]=find(x[0])
if d1.get(x[0],0)==0:
l.append(i)
d1[x[0]]=1
else:
u,v=find(x[0]),find(x[1])
if (d1.get(u,0)==1 and d1.get(v,0)==1):
continue
elif u==v:
continue
elif d1.get(u,0)==1:
d1[v]=1
l.append(i)
elif d1.get(v,0)==1:
d1[u]=1
l.append(i)
else:
px=u
py=v
if r[px]>r[py]:
p[py]=px
elif r[px]<r[py]:
p[px]=py
else:
p[py]=px
r[px]+=1
l.append(i)
stdout.write(str(power(len(l)))+" "+str(len(l))+'\n')
for i in l:
stdout.write(str(i+1)+" ")
| 12 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)
template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);}
template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);}
template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); }
template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n); for(auto& a : v) init(a, x, m...); }
const ll MOD = 1e9+7;
template<class T> T powmod(T x, ll n, ll m)
{
T r = 1;
T a = x % m;
while (n>0)
{
if (n & 1)
r = (r*a)%m;
a = (a*a)%m;
n = n >> 1;
}
return r;
}
int get(int x, vector<int>& mp){
if (x == mp[x])
return x;
return mp[x] = get(mp[x], mp);
}
int main(int argc, char** argv) {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);
int n, m;
cin >> n >> m;
// n = m = 1e5;
// vector<list<int>> L(m+1);
set<pii> S;
vector<pii> v(n+1);
vector<bool> skip(n+1);
FOR1(i, n){
int k;
#if 0
if (i < n) {
v[i].first = i;
v[i].second = i + 1;
}
else{
v[i].first = 1;
v[i].second = i;
}
#else
cin >> k;
if(k==1) {
cin >> v[i].first;
v[i].second = m+1;
}
else
cin >> v[i].first >> v[i].second;
if (v[i].first > v[i].second)
swap(v[i].first, v[i].second);
#endif
if (!S.count(v[i])){
S.insert(v[i]);
// L[v[i].first].push_back(i);
// if (v[i].second != m+1)
// L[v[i].second].push_back(i);
}
else
skip[i] = 1;
}
vector<int> sol;
vector<int> mp(m+2);
vector<bool> done(m+2); done[m+1] = 1;
iota(mp.begin(), mp.end(), 0);
S.clear();
FOR1(i, n){
if (skip[i]) continue;
int x = get(v[i].first, mp);
int y = get(v[i].second, mp);
if (x == y) continue;
if (S.count({x, y})) continue;
if (done[x] && done[y]) continue;
if (x > y) swap(x, y);
if (!done[x] && !done[y]){
mp[x] = y;
done[x] = 1;
}
else if (!done[x] && done[y]){
done[x] = 1;
}
else{
done[y] = 1;
}
S.insert({x, y});
sol.push_back(i);
}
cout << powmod(2ll, sol.size(), MOD) << " " << sol.size() << endl;
for(auto it : sol) cout << it << " "; cout << endl;
if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
return 0;
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
class DSU {
private:
vector <int> par, sz;
int n;
public:
DSU(int _n) : n(_n) {
sz.assign(n + 1, 1);
par.assign(n + 1, 0);
iota(par.begin(), par.end(), 0);
}
int find_parent(int a) {
if(a == par[a]) return a;
return par[a] = find_parent(par[a]);
}
bool union_sets(int a, int b) {
a = find_parent(a);
b = find_parent(b);
if(a != b) {
par[b] = a;
sz[a] += sz[b];
return 1;
}
return 0;
}
};
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n, m;
cin >> n >> m;
DSU dsu(m + 1);
vector <int> ans;
int res = 1;
for(int i = 1; i <= n; i++) {
int k;
cin >> k;
int a, b;
cin >> a;
if(k > 1) cin >> b;
else b = m + 1;
if(dsu.union_sets(a, b)) {
res = (2LL * res) % MOD;
ans.push_back(i);
}
}
cout << res << ' ' << ans.size() << '\n';
for(int &x : ans) {
cout << x << ' ';
}
}
| 12 |
CPP
|
from collections import defaultdict
import sys
input = sys.stdin.readline
class UF:
def __init__(self, N):
self.par = list(range(N))
self.sz = [1] * N
def find(self, x):
if self.par[x] != x:
self.par[x] = self.find(self.par[x])
return self.par[x]
def union(self, x, y):
xr, yr = self.find(x), self.find(y)
if xr == yr:
return False
if self.sz[xr] < self.sz[yr]:
xr, yr = yr, xr
self.par[yr] = xr
self.sz[xr] += self.sz[yr]
return True
def size(self, x):
return self.sz[self.find(x)]
def connected(self, x, y):
return self.find(x) == self.find(y)
n,m = list(map(int, input().split(' ')))
cur_count = 1
cur_lst = []
uf = UF(m+2)
for i in range(n):
bit_lst = list(map(int, input().split(' ')))
bit1 = bit_lst[1]
bit2 = m+1
if bit_lst[0] == 2:
bit2 = bit_lst[2]
if uf.union(bit1, bit2):
cur_lst.append(str(i+1))
cur_count *= 2
cur_count %= 10**9 + 7
print(cur_count, len(cur_lst))
print(" ".join(cur_lst))
| 12 |
PYTHON3
|
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
typedef pair<int, int> ii;
int p[500001];
int find(int a) {
if (a == p[a]) return a;
else return p[a] = find(p[a]);
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a != b) {
p[a] = b;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> one(m, 0);
vector<vector<int>> two(m);
vector<int> ans;
for (int i = 0; i <= m; ++i) p[i] = i;
int k, a, b;
for (int i = 0; i < n; ++i) {
cin >> k;
if (k == 1) {
cin >> a;
int aa = find(a);
if (aa != 0) {
ans.push_back(i);
// deleta a
p[aa] = 0;
}
}
else {
cin >> a >> b;
int aa = find(a), bb = find(b);
if (aa == 0 && bb == 0) continue;
else if (aa == 0) {
ans.push_back(i);
// deleta b
p[bb] = 0;
}
else if (bb == 0) {
ans.push_back(i);
// deleta a
p[aa] = 0;
}
else {
if (aa != bb) {
merge(a, b);
ans.push_back(i);
}
}
}
}
long long p = 1;
for (int i = 0; i < ans.size(); ++i) {
(p *= 2) %= MOD;
}
cout << p << ' ' << ans.size() << '\n';
for (int u: ans) cout << u + 1 << ' ';
cout << '\n';
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define benq queue
#define pbenq priority_queue
#define all(x) x.begin(), x.end()
#define sz(x) (ll)x.size()
#define m1(x) memset(x, 1, sizeof(x))
#define m0(x) memset(x, 0, sizeof(x))
#define inf(x) memset(x, 0x3f, sizeof(x))
#define MOD 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define PI 3.14159265358979323846264338
#define flout cout << fixed << setprecision(12)
ll p[500007], r[500007], n, m;
vector<ll> v;
ll get(ll x) {return p[x] = (p[x] == x ? x : get(p[x]));}
void uni(ll x, ll y) {
x = get(x);
y = get(y);
if(x == y) return;
if(r[x] == r[y]) r[x]++;
if(r[x] > r[y]) swap(x, y);
p[x] = y;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
iota(p + 1, p + m + 1, 1);
for(ll i = 1; i <= n; ++i) {
ll k, a, b = m + 1;
cin >> k >> a;
if(k == 2) cin >> b;
if(get(a) != get(b)) {
uni(a, b);
v.push_back(i);
}
}
ll ans = 1;
for(ll i = 0; i < sz(v); ++i) ans = (ans + ans)%MOD;
cout << ans << ' ' << sz(v) << '\n';
for(ll i : v) cout << i << ' ';
cout << '\n';
}
| 12 |
CPP
|
import sys
input = sys.stdin.buffer.readline
def _find(s, u):
p = []
while s[u] != u:
p.append(u)
u = s[u]
for v in p: s[v] = u
return u
def _union(s, u, v):
su, sv = _find(s, u), _find(s, v)
if su != sv: s[su] = sv
n, m = map(int, input().split())
s, res = list(range(m+1)), []
for i in range(n):
p = list(map(int, input().split()))
if p[0] == 1:
u, v = 0, p[1]
else:
u, v = p[1], p[2]
su, sv = _find(s, u), _find(s, v)
if su != sv:
_union(s, su, sv)
res.append(i+1)
print(pow(2, len(res), 10**9+7), len(res))
print(*res)
| 12 |
PYTHON3
|
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
def find(self, a):
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a:
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b):
self.parent[self.find(b)] = self.find(a)
import sys
input = sys.stdin.readline
n, m = map(int, input().split());UF = UnionFind(m + 1);MOD = 10 ** 9 + 7;out = []
for i in range(1, n + 1):
l = list(map(int, input().split()))
if len(l) == 2:u = 0;v = l[1]
else:_, u, v = l
uu = UF.find(u);vv = UF.find(v)
if uu != vv:UF.union(uu,vv);out.append(i)
print(pow(2, len(out), MOD), len(out));print(' '.join(map(str,out)))
| 12 |
PYTHON3
|
#include <bits/stdc++.h>
//using namespace std;
typedef long long ll;
typedef long double ld;
//std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
//int randi = std::uniform_int_distribution<int>(0, 999)(rng);
const double EPS = 1e-9;
const double PI = acos(-1.0);
const ll OO = 2e18 + 10;
const int oo = 1e9 + 10;
const int MOD = 1e9 + 7;
struct DSU {
std::vector<int> parent, size;
void build(int n) {
parent.resize(n);
size.resize(n);
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
int find(int v) {
if (parent[v] == v)
return v;
return parent[v] = find(parent[v]);
}
bool unite(int v, int u) {
v = find(v);
u = find(u);
if (v == u)
return false;
if (size[u] > size[v])
std::swap(v, u);
parent[u] = v;
size[v] += size[u];
return true;
}
};
void solve() {
int n, m;
std::cin >> n >> m;
DSU dsu;
dsu.build(m + 1);
std::set<int> s;
for (int i = 0; i < n; i++) {
int k;
std::cin >> k;
if (k == 1) {
int x;
std::cin >> x;
if (dsu.find(x) != dsu.find(0)) {
dsu.unite(x, 0);
s.insert(i);
}
} else {
int x1, x2;
std::cin >> x1 >> x2;
if (dsu.find(x1) != dsu.find(x2)) {
dsu.unite(x1, x2);
s.insert(i);
}
}
}
ll ans = 1;
for (int i = 0; i < s.size(); i++) {
ans = (ans * 2) % MOD;
}
std::cout << ans << " " << s.size() << "\n";
for (auto &i : s) {
std::cout << (i + 1) << " ";
}
std::cout << "\n";
}
int main() {
std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);
// std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
int tests = 1;
// std::cin >> tests;
while (tests--) {
solve();
}
// std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
// std::cout << "time in micros: " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
return 0;
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "/debug.h"
#else
#define db(...)
#endif
#define all(v) v.begin(), v.end()
#define pb push_back
using ll = long long;
const int NAX = 2e5 + 5, MOD = 1000000007;
class UnionFind
{ // OOP style
private:
vector<int> p, rank, setSize; // remember: vi is vector<int>
int numSets;
public:
UnionFind(int N)
{
setSize.assign(N, 1);
numSets = N;
rank.assign(N, 0);
p.assign(N, 0);
for (int i = 0; i < N; i++)
p[i] = i;
}
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
bool unionSet(int i, int j)
{
if (!isSameSet(i, j))
{
numSets--;
int x = findSet(i), y = findSet(j);
// rank is used to keep the tree short
if (rank[x] > rank[y])
{
p[y] = x;
setSize[x] += setSize[y];
}
else
{
p[x] = y;
setSize[y] += setSize[x];
if (rank[x] == rank[y])
rank[y]++;
}
return true;
}
return false;
}
int numDisjointSets() { return numSets; }
int sizeOfSet(int i) { return setSize[findSet(i)]; }
};
void solveCase()
{
int n, m;
cin >> n >> m;
UnionFind u(m + 1);
vector<int> res;
int val = 1;
for (size_t i = 0; i < n; i++)
{
int k;
cin >> k;
int a = 0, b = 0;
cin >> a;
if (k > 1)
cin >> b;
if (u.unionSet(a, b))
{
res.pb(i + 1);
val += val;
if (val >= MOD)
val -= MOD;
}
}
cout << val << ' ';
cout << res.size() << '\n';
for (auto &x : res)
cout << x << ' ';
cout << '\n';
}
int32_t main()
{
#ifndef LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
int t = 1;
// cin >> t;
for (int i = 1; i <= t; ++i)
solveCase();
return 0;
}
| 12 |
CPP
|
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
const int maxn=5e5+5;
int f[maxn],ans[maxn];
int _find(int x){return x!=f[x]?f[x]=_find(f[x]):f[x];}
int main(){
int n,m,id=0,num=1;scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)f[i]=i;
for(int i=1;i<=n;i++){
int k,a,b;scanf("%d",&k);
if(k==1)a=0,scanf("%d",&b);
else scanf("%d%d",&a,&b);
int aa=_find(a),bb=_find(b);
if(aa!=bb){
f[aa]=bb;
ans[id++]=i;
(num*=2)%=mod;
}
}
printf("%d %d\n",num,id);
for(int i=0;i<id;i++)printf("%d ",ans[i]);printf("\n");
return 0;
}
| 12 |
CPP
|
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <deque>
#include <set>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <ctime>
#include <queue>
#include <stack>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <fstream>
#include <bitset>
#include <complex>
#include <numeric>
//#include "utils/haha.h"
//#include "utils/max_flow.h"
using namespace std;
typedef pair<int, int> PII;
typedef pair<string, string> PSS;
typedef pair<string, int> PSI;
typedef pair<int, PII> PIP;
typedef long long ll;
typedef pair<int, ll> PIL;
typedef pair<ll, ll> PLL;
typedef pair<double, double> PDD;
typedef pair<ll, PII> PLP;
template<typename T>
inline T read_by_char() {
T s = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
s = (s << 3) + (s << 1) + ch - 48;
ch = getchar();
}
return s * f;
}
#define ri() read_by_char<int>()
#define rl() read_by_char<ll>()
#define CLS(x, v) (memset((x), (v), sizeof((x))))
template<class TH>
void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; }
template<class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
while (*sdbg != ',')cerr << *sdbg++;
cerr << '=' << h << ',';
_dbg(sdbg + 1, a...);
}
template<class T>
ostream &operator<<(ostream &os, set<T> V) {
os << "[";
for (auto vv : V) os << vv << ",";
return os << "]";
}
template<class T>
ostream &operator<<(ostream &os, vector<T> V) {
os << "[";
for (auto vv : V) os << vv << ",";
return os << "]";
}
template<class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
#ifdef _zzz_
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif
template<class T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<class T>
using max_heap = priority_queue<T>;
//const int N = 1e6 + 1e5 + 10;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
struct PairHash {
template<typename T1, typename T2>
std::size_t operator()(const pair<T1, T2> &p) const {
return hash<T1>()(p.first) ^ hash<T2>()(p.second);
}
};
template<unsigned MOD_>
struct ModInt {
static constexpr unsigned MOD = MOD_;
unsigned x;
void undef() { x = (unsigned) -1; }
bool isnan() const { return x == (unsigned) -1; }
inline int geti() const { return (int) x; }
ModInt() { x = 0; }
ModInt(const ModInt &y) { x = y.x; }
ModInt(int y) {
if (y < 0 || (int) MOD <= y) y %= (int) MOD;
if (y < 0) y += MOD;
x = y;
}
ModInt(unsigned y) { if (MOD <= y) x = y % MOD; else x = y; }
ModInt(long long y) {
if (y < 0 || MOD <= y) y %= MOD;
if (y < 0) y += MOD;
x = y;
}
ModInt(unsigned long long y) { if (MOD <= y) x = y % MOD; else x = y; }
ModInt &operator+=(const ModInt y) {
if ((x += y.x) >= MOD) x -= MOD;
return *this;
}
ModInt &operator-=(const ModInt y) {
if ((x -= y.x) & (1u << 31)) x += MOD;
return *this;
}
ModInt &operator*=(const ModInt y) {
x = (unsigned long long) x * y.x % MOD;
return *this;
}
ModInt &operator/=(const ModInt y) {
x = (unsigned long long) x * y.inv().x % MOD;
return *this;
}
ModInt operator-() const { return (x ? MOD - x : 0); }
ModInt inv() const { return pow(MOD - 2); }
ModInt pow(long long y) const {
ModInt b = *this, r = 1;
if (y < 0) {
b = b.inv();
y = -y;
}
for (; y; y >>= 1) {
if (y & 1) r *= b;
b *= b;
}
return r;
}
friend ModInt operator+(ModInt x, const ModInt y) { return x += y; }
friend ModInt operator-(ModInt x, const ModInt y) { return x -= y; }
friend ModInt operator*(ModInt x, const ModInt y) { return x *= y; }
friend ModInt operator/(ModInt x, const ModInt y) { return x *= y.inv(); }
friend bool operator<(const ModInt x, const ModInt y) { return x.x < y.x; }
friend bool operator==(const ModInt x, const ModInt y) { return x.x == y.x; }
friend bool operator!=(const ModInt x, const ModInt y) { return x.x != y.x; }
};
const unsigned int mod = 1e9 + 7;
typedef ModInt<mod> mod_int;
void update(int &v, int nv) {
if (nv == -1) return;
if (v < 0 || v > nv) v = nv;
}
struct Disjoint {
Disjoint(int n) {
p.assign(n + 1, -1);
}
vector<int> p;
bool join(int a, int b) {
int ra = root(a);
int rb = root(b);
if (ra == rb) return false;
int sum = p[ra] + p[rb];
if (p[ra] < p[rb]) {
p[rb] = ra;
p[ra] = sum;
} else {
p[ra] = rb;
p[rb] = sum;
}
return true;
}
int root(int x) {
return p[x] < 0 ? x : (p[x] = root(p[x]));
}
};
void solve(int ncase) {
int n = ri(), m = ri();
Disjoint djs(m + 1);
int cnt = 0;
vector<int> a;
for (int i = 0; i < n; i++) {
int k = ri();
int b[2] = {0, 0};
for (int j = 0; j < k; j++) b[j] = ri();
if (djs.join(b[0], b[1])) {
a.push_back(i + 1);
}
}
mod_int ret = mod_int(2).pow(a.size());
printf("%d %d\n", ret.geti(), a.size());
for (int i = 0; i < a.size(); i++) printf("%d%c", a[i], " \n"[i + 1 == a.size()]);
}
void solve_all_cases() {
int T = 1;
//scanf("%d", &T);
//cin >> T;
//T = ri();
int ncase = 0;
// pre_calc();
while (T--) {
solve(++ncase);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << std::fixed;
cout << setprecision(9);
#ifdef _zzz_
//ios_base::sync_with_stdio(true);
freopen("C:\\Users\\grain\\Desktop\\in.txt", "r", stdin);
//auto x = freopen("C:\\Users\\grain\\Desktop\\out.txt", "w", stdout);
//cerr << x << " " << errno << endl;
auto start_time = clock();
#endif
solve_all_cases();
//test();
#ifdef _zzz_
cout << (clock() - start_time) * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;
#endif
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
*/
| 12 |
CPP
|
#include<bits/stdc++.h>
#define pb emplace_back
#define AI(i) begin(i), end(i)
using namespace std;
using ll = long long;
template<class T>
bool chmax(T &val, T nv) { return val < nv ? (val = nv, true) : false; }
template<class T>
bool chmin(T &val, T nv) { return nv < val ? (val = nv, true) : false; }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() {cerr << endl;}
template<class T1, class ...T2>
void kout (T1 v, T2 ...e) { cerr << v << ' ', kout(e...); }
template<class T>
void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L)==R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// What I should check
// 1. overflow
// 2. corner cases
// Enjoy the problem instead of hurrying to AC
// Good luck !
const int MAX_N = 500010, p = 1e9 + 7;
int n, m;
vector<vector<int>> vs;
struct dsu {
vector<int> g, sz, mxv;
dsu() {}
dsu(int n) { g.resize(n+1), sz.resize(n+1, 1), mxv.resize(n+1), iota(AI(g), 0), iota(AI(mxv), 0); }
int F(int i) { return i == g[i] ? i : g[i] = F(g[i]); }
bool M(int a, int b) {
a = F(a), b = F(b);
if (a == b) return false;
if (sz[a] < sz[b]) swap(a, b);
return g[b] = a, sz[a] += sz[b], chmax(mxv[a], mxv[b]), true;
}
int operator()(int i) { return F(i); }
};
ll bin_pow(ll v, ll t) {
ll res = 1;
for (;t;t>>=1, v = v * v % p)
if (t&1) res = res * v % p;
return res;
}
int pa[MAX_N];
int getpa(int i) { return i == pa[i] ? i : pa[i] = getpa(pa[i]); }
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
vs.resize(n);
for (auto &v : vs) {
int len;
cin >> len;
v.resize(len);
for (auto &x : v) cin >> x;
sort(AI(v));
}
iota(pa, pa+m+5, 0);
vector<int> res;
int bad = m + 2;
for (int i = 0;i < n;++i) {
auto &vec = vs[i];
for (int &u : vec) u = getpa(u);
sort(AI(vec));
while (vec.size() && vec.back() == bad) vec.pop_back();
if (vec.empty() || (vec.size() == 2 && vec[0] == vec[1])) continue;
if (vec.size() == 1)
pa[vec[0]] = bad;
else
pa[vec[0]] = vec[1];
res.pb(i);
}
// for (int i = 0;i < n;++i) {
// if (vs[i].size() > 1) D.M(vs[i][0], vs[i][1]);
// }
//
// {
// vector<vector<int>> lisan(m + 1);
// for (int i = 0;i < n;++i) {
// auto &vec = lisan[ D(vs[i][0]) ];
// vec.insert(end(vec), AI(vs[i]));
// }
// for (auto &vec : lisan) sort(AI(vec)), vec.resize(unique(AI(vec)) - begin(vec));
//
// for (int i = 0;i < n;++i) {
// auto &vec = lisan[ D(vs[i][0]) ];
// for (auto &u : vs[i]) u = lower_bound(AI(vec), u) - begin(vec);
// }
// }
// // lisan is done
// //
// // then I need to calculate thing
// vector<int> res;
//
// vector<dsu> DS(m+1);
// for (int i = 0;i < n;++i) {
// }
cout << bin_pow(2, res.size()) << ' ' << res.size() << '\n';
for (int u : res) cout << u+1 << " \n"[u==res.back()];
}
| 12 |
CPP
|
/// In the name of God
#include <bits/stdc++.h>
#define FILE_NAME "A"
using namespace std;
using ll = long long;
#define f first
#define s second
#define pb push_back
#define pp pop_back
#define SZ(x) ((int) x.size())
#define all(x) x.begin(), x.end()
#define what_is(x) cerr << #x << " is " << x << endl;
void freekick() {
#ifndef ONLINE_JUDGE
freopen(FILE_NAME".in", "r", stdin);
freopen(FILE_NAME".out", "w", stdout);
#endif
}
const int N = 2e5 + 112;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const int DX[] = {+1, 0, -1, 0, +1, +1, -1, -1};
const int DY[] = {0, +1, 0, -1, +1, -1, +1, -1};
void freegoal() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
struct DSU {
vector<int> parent;
DSU(int n) {
parent.resize(n, -1);
}
int find_set(int v) {
if (parent[v] < 0)
return v;
return parent[v] = find_set(parent[v]);
}
int find_set2(int v) {
return 0 > parent[v] ? v : parent[v] = find_set2(parent[v]);
}
bool unite(int a, int b) {
a = find_set2(a);
b = find_set2(b);
if (a == b)
return 0;
if (parent[a] < parent[b])
swap(a, b);
parent[b] += parent[a];
parent[a] = b;
return 1;
}
};
signed main() {
freegoal();
int n, m, x, y;
cin >> n >> m;
vector<int> result;
DSU dsu(m + 1);
ll answer = 1;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
if (k == 1) {
cin >> x;
y = 0;
} else {
cin >> x >> y;
}
auto count_primes = [&](int n) {
const int S = 10000;
vector<int> primes;
int nsqrt = sqrt(n);
vector<char> is_prime(nsqrt + 1, true);
for (int i = 2; i <= nsqrt; i++) {
if (is_prime[i]) {
primes.push_back(i);
for (int j = i * i; j <= nsqrt; j += i)
is_prime[j] = false;
}
}
int result = 0;
vector<char> block(S);
for (int k = 0; k * S <= n; k++) {
fill(block.begin(), block.end(), true);
int start = k * S;
for (int p : primes) {
int start_idx = (start + p - 1) / p;
int j = max(start_idx, p) * p - start;
for (; j < S; j += p)
block[j] = false;
}
if (k == 0)
block[0] = block[1] = false;
for (int i = 0; i < S && start + i <= n; i++) {
if (block[i])
result++;
}
}
return result;
};
if (dsu.unite(x, y)) {
result.pb(i + 1);
answer = answer * 2;
answer = answer % MOD;
}
}
cout << answer << " " << SZ(result) << "\n";
for (auto &r : result)
cout << r << " ";
cout << "\n";
return false;
}
| 12 |
CPP
|
#include <bits/stdc++.h>
#define endl '\n'
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define LL long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;
typedef pair<int,int>pii;
const int mod=1e9+7;
int n,m;
vector<int>ans;
int fa[500010];
int find(int a){
return fa[a]==a?a:fa[a]=find(fa[a]);
}
bool unite(int a,int b){
a=find(a),b=find(b);
fa[a]=b;
return a!=b;
}
void init(int n){
for(int i=1;i<=n;i++)fa[i]=i;
}
int main() {
cin>>n>>m;
init(m+1);
for(int i=1,k;i<=n;i++){
scanf("%d",&k);
int fa;
scanf("%d",&fa);
int fb=m+1;
if(k>1)scanf("%d",&fb);
if(unite(fa,fb))ans.pb(i);
}
int res=1;
for(int i=0;i<ans.size();i++)res=res*2%mod;
printf("%d %d\n",res,(int)ans.size());
for(auto v:ans)printf("%d ",v);
puts("");
return 0;
}
| 12 |
CPP
|
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 500005;
const ll MOD = 1000000007;
int T,n,m,tot;
int fa[MAXN];
int ans[MAXN];
int getroot(int u)
{
return u == fa[u] ? u : fa[u] = getroot(fa[u]);
}
int main()
{
scanf("%d%d",&n,&m);
for (int i = 0;i <= m;i++)
fa[i] = i;
for (int k,u,v,i = 1;i <= n;i++)
{
scanf("%d",&k);
if (k == 1)
{
scanf("%d",&u);
v = 0;
}
else
scanf("%d%d",&u,&v);
int ru = getroot(u),rv = getroot(v);
if (ru != rv)
{
ans[++tot] = i;
fa[ru] = rv;
}
}
ll pw = 1;
for (int i = 1;i <= tot;i++)
(pw *= 2) %= MOD;
printf("%lld %d\n",pw,tot);
for (int i = 1;i <= tot;i++)
printf("%d ",ans[i]);
printf("\n");
return 0;
}
| 12 |
CPP
|
// author: erray
#include<bits/stdc++.h>
using namespace std;
// modular template by tourist
template <typename T>
T inverse(T a, T m) {
T u = 0, v = 1;
while (a != 0) {
T t = m / a;
m -= t * a; swap(a, m);
u -= t * v; swap(u, v);
}
assert(m == 1);
return u;
}
template <typename T>
class Modular {
public:
using Type = typename decay<decltype(T::value)>::type;
constexpr Modular() : value() {}
template <typename U>
Modular(const U& x) {
value = normalize(x);
}
template <typename U>
static Type normalize(const U& x) {
Type v;
if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
else v = static_cast<Type>(x % mod());
if (v < 0) v += mod();
return v;
}
const Type& operator()() const { return value; }
template <typename U>
explicit operator U() const { return static_cast<U>(value); }
constexpr static Type mod() { return T::value; }
Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; }
Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; }
template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
Modular& operator++() { return *this += 1; }
Modular& operator--() { return *this -= 1; }
Modular operator++(int) { Modular result(*this); *this += 1; return result; }
Modular operator--(int) { Modular result(*this); *this -= 1; return result; }
Modular operator-() const { return Modular(-value); }
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {
#ifdef _WIN32
uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value);
uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m;
asm(
"divl %4; \n\t"
: "=a" (d), "=d" (m)
: "d" (xh), "a" (xl), "r" (mod())
);
value = m;
#else
value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
#endif
return *this;
}
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type& operator*=(const Modular& rhs) {
int64_t q = static_cast<int64_t>(static_cast<long double>(value) * rhs.value / mod());
value = normalize(value * rhs.value - q * mod());
return *this;
}
template <typename U = T>
typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
value = normalize(value * rhs.value);
return *this;
}
Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }
template <typename U>
friend const Modular<U>& abs(const Modular<U>& v) { return v; }
template <typename U>
friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend std::istream& operator>>(std::istream& stream, Modular<U>& number);
private:
Type value;
};
template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; }
template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; }
template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; }
template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template<typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
assert(b >= 0);
Modular<T> x = a, res = 1;
U p = b;
while (p > 0) {
if (p & 1) res *= x;
x *= x;
p >>= 1;
}
return res;
}
template <typename T>
bool IsZero(const Modular<T>& number) {
return number() == 0;
}
template <typename T>
string to_string(const Modular<T>& number) {
return to_string(number());
}
template <typename T>
std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) {
return stream << number();
}
template <typename T>
std::istream& operator>>(std::istream& stream, Modular<T>& number) {
typename common_type<typename Modular<T>::Type, int64_t>::type x;
stream >> x;
number.value = Modular<T>::normalize(x);
return stream;
}
/*
using ModType = int;
struct VarMod { static ModType value; };
ModType VarMod::value;
ModType& md = VarMod::value;
using Mint = Modular<VarMod>;
*/
constexpr int md = (int) 1e9 + 7;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
class dsu {
public:
vector<int> par, ct;
dsu(int n) {
par.resize(n);
ct.resize(n, 1);
iota(par.begin(), par.end(), 0);
}
inline int get(int v) {
return par[v] = (v == par[v] ? v : get(par[v]));
}
inline bool unite(int x, int y) {
x = get(x);
y = get(y);
if (x == y) return false;
ct[x] += ct[y];
par[y] = x;
return true;
}
};
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> a(n, make_pair(-1, 0));
for (int i = 0; i < n; ++i) {
int k;
cin >> k;
cin >> a[i].first;
if (k == 2) {
cin >> a[i].second;
}
--a[i].first;
--a[i].second;
}
vector<bool> taken(m);
vector<int> ans;
dsu d(m);
for (int i = 0; i < n; ++i) {
if (a[i].second == -1) {
if (taken[d.get(a[i].first)] == false){
ans.push_back(i);
taken[d.get(a[i].first)] = true;
}
} else {
auto[v, u] = a[i];
if (taken[d.get(u)]) {
swap(v, u);
}
if ((taken[d.get(v)] && taken[d.get(u)]) || !d.unite(v, u)) {
//no
} else {
ans.push_back(i);
}
}
}
cout << power(Mint(2), (int) ans.size()) << ' ';
cout << (int) ans.size() << '\n';
for (auto el : ans) {
cout << el + 1 << ' ';
}
}
| 12 |
CPP
|
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
mod = 10 ** 9 + 7
base = [0] * (M + 1)
e = [[] for _ in range(M + 1)]
res = []
class UnionFind():
def __init__(self, n):
self.n = n
self.root = [-1] * (n + 1)
self.rnk = [0] * (n + 1)
def Find_Root(self, x):
if self.root[x] < 0:
return x
else:
self.root[x] = self.Find_Root(self.root[x])
return self.root[x]
def Unite(self, x, y):
x = self.Find_Root(x)
y = self.Find_Root(y)
if x == y:
return
elif self.rnk[x] > self.rnk[y]:
self.root[x] += self.root[y]
self.root[y] = x
else:
self.root[y] += self.root[x]
self.root[x] = y
if self.rnk[x] == self.rnk[y]:
self.rnk[y] += 1
def isSameGroup(self, x, y):
return self.Find_Root(x) == self.Find_Root(y)
def Count(self, x):
return -self.root[self.Find_Root(x)]
uf = UnionFind(M)
for i in range(1, N + 1):
a = list(map(int, input().split()))
if a[0] == 1: a.append(0)
u, v = a[1: ]
if uf.isSameGroup(u, v): continue
else:
uf.Unite(u, v)
res.append(i)
print(pow(2, len(res), mod), len(res))
print(*res)
| 12 |
PYTHON3
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#define ll long long
using namespace std;
int n, m;
int f[1234567];
int rrank[1234567];
bool cycle[1234567];
int getfa(int a)
{
if (a != f[a])
f[a] = getfa(f[a]);
return f[a];
}
void connect(int a, int b)
{
int fa = getfa(a), fb = getfa(b);
if (fa == fb)
{
cycle[fa] = true;
return;
}
if (rrank[fa] == rrank[fb])
rrank[fb]++;
if (rrank[fa] > rrank[fb])
swap(fa, fb);
cycle[fb] = cycle[fb] | cycle[fa];
f[fa] = fb;
}
bool connected(int a, int b)
{
int fa = getfa(a), fb = getfa(b);
return (fa == fb) || (cycle[fa] && cycle[fb]);
}
vector<int> v;
ll ans = 1;
ll mod = 1e9 + 7;
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i <= m; i++)
f[i] = i;
for (int i = 0; i < n; i++)
{
int k;
cin >> k;
int a, b;
if (k == 1)
{
cin >> a;
b = a;
if (!cycle[getfa(a)])
{
connect(a, b);
v.push_back(i + 1);
ans *= 2;
if (ans >= mod)
ans -= mod;
}
}
else
{
cin >> a >> b;
if (!connected(a, b))
{
connect(a, b);
v.push_back(i + 1);
ans *= 2;
if (ans >= mod)
ans -= mod;
}
}
}
cout << ans << " " << v.size() << endl;
for (auto x : v)
cout << x << " ";
cout << endl;
return 0;
}
| 12 |
CPP
|
#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (register int i=(x);i<(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define Debug(v) for (auto i:v) cout<<i<<" ";puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fil(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef pair<ll,ll> PA;
typedef vector<int> poly;
inline ll read(){
ll x=0,f=1;char c=getchar();
while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
if (c=='-') f=-1,c=getchar();
while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x*f;
}
const int N = 5e5+10, mod = 1e9+7;
int n,m,f[N],vis[N];
vector<int> ans;
int cnt=1;
int fa[N];
inline int Find(int x){
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
int main(){
n=read(),m=read();
For(i,1,m) fa[i]=i;
For(i,1,n){
int k=read();
if (k==1){
int x=Find(read());
if (!vis[x]) vis[x]=1,ans.pb(i),cnt=2ll*cnt%mod;
} else {
int x=read(),y=read();
x=Find(x),y=Find(y);
if (vis[x]&&vis[y]) continue;
if (x!=y){
ans.pb(i),cnt=2ll*cnt%mod;
fa[x]=y,vis[y]|=vis[x];
}
}
}
printf("%d %d\n",cnt,siz(ans));
for (auto i:ans) printf("%d ",i);
}
| 12 |
CPP
|
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int n,m;
#define P pair<int,int>
P a[N];
int ans[N],nans;
const int mod=1e9+7;
P b[N];
int insert(int x,int y){
if(b[x].first==x&&b[x].second==y)return -1;
if(b[x].first==0){
b[x]=P(x,y);
return y;
}else{
int l=y,r=b[x].second;
if(l==0){
int t=insert(r,l);
if(t!=-1)b[x].second=max(b[x].second,t);
return t;
}
if(r==0){
int t=insert(l,r);
if(t!=-1)b[x].second=max(b[x].second,t);
return t;
}
if(l>r)swap(l,r);
int tmp=insert(l,r);
if(tmp!=-1)b[x].second=max(b[x].second,tmp);
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
int cnt;
cin>>cnt;
if(cnt>=1)cin>>a[i].first;
if(cnt>=2){
cin>>a[i].second;
if(a[i].first>a[i].second)swap(a[i].first,a[i].second);
}
}
for(int i=1;i<=n;i++)if(insert(a[i].first,a[i].second)!=-1)ans[++nans]=i;
long long base=1;
for(int i=1;i<=nans;i++)base=base*2%mod;
cout<<base<<" "<<nans<<endl;
for(int i=1;i<=nans;i++)cout<<ans[i]<<" ";cout<<endl;
return 0;
}
| 12 |
CPP
|
/*
Coded with Leachim's ACM Template.
No errors. No warnings. ~~
*/
#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#pragma GCC diagnostic ignored "-Wsign-compare"
#define LL long long
using namespace std;
const int inf=0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-7;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
const int RT=3;
const int MOD=1e9+7;
const int MAXN=500005;
int binpow(int x,int y,int m) {
int r=1%m;
while(y) {
if(y&1) r=1LL*r*x%m;
x=1LL*x*x%m;
y>>=1;
}
return r;
}
struct edge {
int to,next,w;
}e[MAXN<<1];
int tot,head[MAXN];
void add(int x,int y,int w) {
tot++;
e[tot].to=y;
e[tot].next=head[x];
e[tot].w=w;
head[x]=tot;
}
int fa[MAXN];
vector<int> v;
int a[MAXN];
void dfs(int x,int f) {
a[x]=-1;
for(int p=head[x];p;p=e[p].next) {
int u=e[p].to;
if(a[u]==-1) continue;
dfs(u,x);
}
}
int find(int x) {
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
void join(int x,int y) {
fa[find(x)]=find(y);
}
int x[MAXN],y[MAXN];
void get(int t,int i) {
if(!a[t]) {
a[t]=-1;
v.push_back(i);
} else if(a[t]!=-1) {
v.push_back(i);
dfs(t,t);
}
}
void solve() {
int n,m;
scanf("%d %d", &n, &m);
memset(a+1,0,m*sizeof(a[0]));
for(int i=1;i<=m;i++) fa[i]=i;
for(int i=1;i<=n;i++) {
int k;
scanf("%d", &k);
if(k==1) {
int x;
scanf("%d", &x);
get(x,i);
} else {
scanf("%d %d", &x[i], &y[i]);
if(!a[x[i]] && !a[y[i]]) {
v.push_back(i);
a[x[i]]=a[y[i]]=i;
join(x[i],y[i]);
add(x[i],y[i],i);
add(y[i],x[i],i);
} else if(a[x[i]]==-1) {
get(y[i],i);
} else if(a[y[i]]==-1) {
get(x[i],i);
} else if(!a[x[i]]) {
v.push_back(i);
join(x[i],y[i]);
add(x[i],y[i],i);
add(y[i],x[i],i);
a[x[i]]=i;
} else if(!a[y[i]]) {
v.push_back(i);
join(x[i],y[i]);
add(x[i],y[i],i);
add(y[i],x[i],i);
a[y[i]]=i;
} else {
if(find(x[i])!=find(y[i])) {
v.push_back(i);
join(x[i],y[i]);
add(x[i],y[i],i);
add(y[i],x[i],i);
}
}
}
}
printf("%d %lu\n", binpow(2,v.size(),MOD), v.size());
for(auto x:v) {
printf("%d ", x);
}
puts("");
}
int main() {
int T=1,cas=1;(void)(cas);
// scanf("%d", &T);
while(T--) {
// printf("Case #%d: ", cas++);
solve();
}
return 0;
}
| 12 |
CPP
|
#ifdef LOCAL
//#define _GLIBCXX_DEBUG
#endif
//#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<char> Vc;
typedef vector<P> VP;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;
typedef vector<vector<char>> VVc;
typedef vector<vector<vector<ll>>> VVV;
typedef vector<vector<vector<vector<ll>>>> VVVV;
#define endl '\n'
#define REP(i, a, b) for(ll i=(a); i<(b); i++)
#define PER(i, a, b) for(ll i=(a); i>=(b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF=1e18+18;
const ll MOD=1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a,b)
#define Each(a,b) for(auto &a :b)
#define rEach(i, mp) for (auto i = mp.rbegin(); i != mp.rend(); ++i)
#ifdef LOCAL
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp) cerr << #mp << ":"<<endl; for (auto i = mp.begin(); i != mp.end(); ++i) { cerr << i->first <<":"<<i->second << endl;}
#define dbgset(st) cerr << #st << ":"<<endl; for (auto i = st.begin(); i != st.end(); ++i) { cerr << *i <<" ";}cerr<<endl;
#define dbgarr(n,m,arr) rep(i,n){rep(j,m){cerr<<arr[i][j]<<" ";}cerr<<endl;}
#define dbgdp(n,arr) rep(i,n){cerr<<arr[i]<<" ";}cerr<<endl;
#else
#define dbg(...)
#define dbgmap(...)
#define dbgset(...)
#define dbgarr(...)
#define dbgdp(...)
#endif
#define out(a) cout<<a<<endl
#define out2(a,b) cout<<a<<" "<<b<<endl
#define vout(v) rep(i,v.size()){cout<<v[i]<<" ";}cout<<endl
#define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define fi first
#define se second
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s<<"("<<p.first<<", "<<p.second<<")"; }
template<typename T>istream& operator>>(istream&i,vector<T>&v)
{rep(j,v.size())i>>v[j];return i;}
// vector
template<typename T>
ostream &operator<<(ostream &s, const vector<T> &v) {
int len=v.size();
for(int i=0; i<len; ++i) {
s<<v[i];
if(i<len-1) s<<" ";
}
return s;
}
// 2 dimentional vector
template<typename T>
ostream &operator<<(ostream &s, const vector<vector<T> > &vv) {
s<<endl;
int len=vv.size();
for(int i=0; i<len; ++i) {
s<<vv[i]<<endl;
}
return s;
}
//mint
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%MOD+MOD)%MOD){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator-=(const mint a) {
if ((x += MOD-a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll n) const {
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
// for prime MOD
mint inv() const { return pow(MOD-2);}
mint& operator/=(const mint& a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
typedef vector<mint> Vmint;
typedef vector<vector<mint>> VVmint;
typedef vector<vector<vector<mint>>> VVVmint;
struct combination {
vector<mint> fact, ifact;
combination(int n):fact(n+1),ifact(n+1) {
assert(n < MOD);
fact[0] = 1;
for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
};
//UnionFind
struct UnionFind {
vector<ll> par;
UnionFind(ll n) : par(n, -1) { }
void init(ll n) { par.assign(n, -1); }
ll root(ll x) {
if (par[x] < 0) return x;
else return par[x] = root(par[x]);
}
bool issame(ll x, ll y) {
return root(x) == root(y);
}
bool unite(ll x, ll y) {
x = root(x); y = root(y);
if (x == y) return false;
if (par[x] > par[y]) swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
return true;
}
ll size(ll x) {
return -par[root(x)];
}
};
int solve(){
ll n;
cin>>n;
ll m;
cin>>m;
VV g(n);
rep(i,n){
ll k;
cin>>k;
rep(j,k){
ll id;
cin>>id;
id--;
g[i].pb(id);
}
}
Vec single(m);
Vec singleid;
ll singleroot = -1;
UnionFind uf(m);
Vec ans;
Vec hassingle(m);
dbg(g);
rep(i,n){
Vec &v = g[i];
if(v.size() == 1){
ll x = v[0];
if(singleroot == -1){
singleroot = x;
hassingle[uf.root(x)] = 1;
ans.push_back(i);
}else{
dbg(x);
dbg(uf.root(x));
dbg(hassingle[uf.root(x)]);
if(hassingle[uf.root(x)] == 1) continue;
if(hassingle[uf.root(singleroot)]){
hassingle[uf.root(x)] = 1;
}
if(hassingle[uf.root(x)]){
hassingle[uf.root(singleroot)] = 1;
}
uf.unite(singleroot, x);
hassingle[uf.root(x)] = 1;
ans.push_back(i);
}
}else{
ll x = v[0];
ll y = v[1];
if(uf.root(x) == uf.root(y)) continue;
if(hassingle[uf.root(x)]){
hassingle[uf.root(y)] = 1;
}
if(hassingle[uf.root(y)]){
hassingle[uf.root(x)] = 1;
}
uf.unite(x,y);
ans.push_back(i);
}
}
sort(ALL(ans));
cout << mint(2).pow(ans.size()) << " " << ans.size() << endl;
rep(i,ans.size()){
cout<<ans[i]+1<<" ";
}
cout<<endl;
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout<<std::setprecision(10);
// ll T;
// cin>>T;
// while(T--)
solve();
}
| 12 |
CPP
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define dbg(x) cout <<#x<<":"<<x<<endl;
#define dbgpo(x, y) cout<<"("<<x<<", "<<y<<")";
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define endl '\n'
#define mod 1000000007
// #define int long long
#define vi vector<int>
inline int parent(vi &par,int i){
while(i!=par[i]) {
par[i] = par[par[i]];
i = par[i];
}
return i;
}
inline void union1(vi &par, vi &sz,int p1, int p2) {
if(sz[p1]<sz[p2]){
par[p1] = p2;
sz[p2] += sz[p1];
}
else {
par[p2] = p1;
sz[p1] += sz[p2];
}
}
signed main() {
fast;
int n, dim;
cin >> n >> dim;
vector<int> par(dim+1), sz(dim+1,1);
iota(par.begin(),par.end(),0);
vector<int> ans;
for(int i = 1; i <=n; ++i) {
int k; cin >> k;
int a,b;
if(k==1)
cin >> a,b=0;
if(k==2)
cin >> a >> b;
a = parent(par,a);
b = parent(par,b);
if(a!=b) {
ans.pb(i);
union1(par,sz,a,b);
}
}
vector<int> p2(dim+1);
p2[0] = 1;
for(int i=1;i<=dim;++i)
p2[i] = (p2[i-1]<<1)%mod;
ll ansVal = 1;
for(int i=0;i<=dim;++i)
if(par[i] == i) {
ansVal *= ll(p2[sz[i]-1])%mod;
ansVal %= mod;
}
cout << ansVal << ' ' << ans.size() << endl;
for(auto &x: ans)
cout << x << ' ';
}
| 12 |
CPP
|
import sys
input = sys.stdin.readline
mod=10**9+7
n,m=map(int,input().split())
Group = [i for i in range(m+1)]
GroupOne = [0]*(m+1)
Nodes = [1]*(m+1)
def find(x):
while Group[x] != x:
x=Group[x]
return x
def Union(x,y):
if find(x) != find(y):
if Nodes[find(x)] < Nodes[find(y)]:
Nodes[find(y)] += Nodes[find(x)]
Nodes[find(x)] = 0
if GroupOne[find(x)]==1 or GroupOne[find(y)]==1:
GroupOne[find(x)]=1
GroupOne[find(y)]=1
Group[find(x)] = find(y)
else:
Nodes[find(x)] += Nodes[find(y)]
Nodes[find(y)] = 0
if GroupOne[find(x)]==1 or GroupOne[find(y)]==1:
GroupOne[find(x)]=1
GroupOne[find(y)]=1
Group[find(y)] = find(x)
ANS=[]
for i in range(n):
V=tuple(map(int,input().split()))
if V[0]==1:
x=V[1]
if GroupOne[find(x)]==0:
ANS.append(i+1)
GroupOne[find(x)]=1
else:
continue
else:
x,y=V[1],V[2]
if find(x)==find(y) or (GroupOne[find(x)]==1 and GroupOne[find(y)]==1):
continue
else:
Union(x,y)
ANS.append(i+1)
print(pow(2,len(ANS),mod),len(ANS))
print(*ANS)
| 12 |
PYTHON3
|
#include<bits/stdc++.h>
#define fi first
#define se second
#define LL long long
#define PI std::pair<int,int>
#define MP std::make_pair
const int N=500005,INF=0x8000000;
int n,m,fa[N];
std::vector<int>ans;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
int k,x,y;
scanf("%d%d",&k,&x);
if(k==1)y=INF;
else{
scanf("%d",&y);
if(x==y)y=INF;
else if(x>y)std::swap(x,y);
}
while(1){
if(x==INF)break;
if(!fa[x]){
fa[x]=y==INF?-1:y;
ans.push_back(i);
break;
}
if(fa[x]==-1){
x=y;
y=INF;
continue;
}
if(fa[fa[x]])fa[x]=fa[fa[x]];
if(fa[x]==-1){
x=y;
y=INF;
continue;
}
x=fa[x];
if(x==y)break;
if(x>y)std::swap(x,y);
}
}
int uans=1;
for(int i=0;i<ans.size();i++)uans=uans*2%1000000007;
printf("%d %d\n",uans,(int)ans.size());
for(int x:ans)printf("%d ",x);
puts("");
}
| 12 |
CPP
|
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template<typename... T> void rd(T&... args) {((cin>>args), ...);}
template<typename... T> void wr(T... args) {((cout<<args<<" "), ...);cout<<endl;}
struct UF {
vector<int> fa, sz;
vector<bool> single;
UF(int n) : fa(n), sz(n, 1), single(n) {
iota(all(fa), 0);
}
int find(int x) {
return fa[x]==x ? x : fa[x]=find(fa[x]);
}
bool join(int x, int y) {
x=find(x), y=find(y);
if (x==y) return false;
if (single[x] && single[y]) return false;
if (sz[x]>sz[y]) swap(x, y);
fa[x]=y;
sz[y]+=sz[x];
single[y]=single[y] || single[x];
return true;
}
};
constexpr int mod=1e9+7;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin>>n>>m;
UF uf(m);
vector<int> s;
for (int i=0; i<n; i++) {
int k;
cin>>k;
if (k==1) {
int x;
cin>>x;
x--;
x=uf.find(x);
if (!uf.single[x]) {
uf.single[x]=true;
s.push_back(i);
}
} else {
int x, y;
cin>>x>>y;
x--, y--;
if (uf.join(x, y)) {
s.push_back(i);
}
}
}
ll ans=1;
for (int i=0; i<(int)s.size(); i++) {
ans=2*ans%mod;
}
cout<<ans<<' '<<s.size()<<'\n';
for (auto x:s) cout<<x+1<<' ';
return 0;
}
| 12 |
CPP
|
#include<bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int N, M; cin >> N >> M;
const int MOD = 1e9 + 7;
int tot = 1;
vector<int> ans;
vector<int> par(M); iota(par.begin(), par.end(), 0);
vector<bool> good(M);
auto get_par = [&](int v) {
while (v != par[v]) {
v = par[v] = par[par[v]];
}
return v;
};
auto merge = [&](int v, int u) {
v = get_par(v);
u = get_par(u);
if (v != u) {
par[u] = v;
if (good[u]) good[v] = true;
return true;
} else return false;
};
for (int i = 0; i < N; ++i) {
int sz; cin >> sz;
vector<int> A(sz);
for (int &a : A) cin >> a, --a;
if (sz == 1) {
int a = A[0];
if (!good[get_par(a)]) {
good[get_par(a)] = true;
tot += tot;
if (tot >= MOD) tot -= MOD;
ans.emplace_back(i);
}
} else {
int a = A[0], b = A[1];
if ((!good[get_par(a)] || !good[get_par(b)]) && merge(a, b)) {
tot += tot;
if (tot >= MOD) tot -= MOD;
ans.emplace_back(i);
}
}
}
cout << tot << ' ' << int(ans.size()) << '\n';
for (int i : ans) cout << i + 1 << ' ';
return 0;
}
| 12 |
CPP
|
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <string>
#include <stack>
#include <queue>
#define ll long long
#define pii pair<int, int>
#define maxn 500005
#define mod 1000000007
#define Goodbye2020 ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
struct edge{
int a, b, ind;
edge(int x, int y, int z) {
a = x, b = y, ind = z;
}
};
vector<edge> v;
vector<int> ans;
int par[maxn];
int find(int a) {
return (a == par[a] ? a : par[a] = find(par[a]));
}
void Union(int a, int b) {
par[find(a)] = find(b);
}
ll modpow(int p) {
ll ret = 1, mult = 2;
while (p) {
if (p & 1) ret = (ret * mult) % mod;
mult = (mult * mult) % mod;
p >>= 1;
}
return ret;
}
int main() {
Goodbye2020
int n, m;
cin >> n >> m;
for (int i = 0;i <= m;i++) par[i] = i;
for (int i = 0;i < n;i++) {
int k;
cin >> k;
int ed[2] = {0, 0};
for (int j = 0;j < k;j++) {
cin >> ed[j];
}
v.push_back(edge(ed[0], ed[1], i));
}
int cnt = n;
for (int i = 0;i < n;i++) {
if (find(v[i].a) != find(v[i].b)) {
Union(v[i].a, v[i].b);
ans.push_back(v[i].ind + 1);
} else {
cnt--;
}
}
cout << modpow(cnt) << " " << ans.size() << "\n";
for (int i = 0;i < ans.size();i++) cout << ans[i] << " ";
cout << endl;
}
| 12 |
CPP
|
import sys
readline = sys.stdin.readline
class UF():
def __init__(self, num):
self.par = [-1]*num
self.color = [0]*num
def find(self, x):
if self.par[x] < 0:
return x
else:
stack = []
while self.par[x] >= 0:
stack.append(x)
x = self.par[x]
for xi in stack:
self.par[xi] = x
return x
def col(self, x):
return self.color[self.find(x)]
def paint(self, x):
self.color[self.find(x)] = 1
def union(self, x, y):
rx = self.find(x)
ry = self.find(y)
if rx != ry:
if self.par[rx] > self.par[ry]:
rx, ry = ry, rx
self.par[rx] += self.par[ry]
self.par[ry] = rx
self.color[rx] |= self.color[ry]
return True
return False
N, M = map(int, readline().split())
MOD = 10**9+7
ans = []
T = UF(M)
for m in range(N):
k, *x = map(int, readline().split())
if k == 1:
u = x[0]-1
if not T.col(u):
ans.append(m+1)
T.paint(u)
else:
u, v = x[0]-1, x[1]-1
if T.col(u) and T.col(v):
continue
if T.union(u, v):
ans.append(m+1)
print(pow(2, len(ans), MOD), len(ans))
print(' '.join(map(str, ans)))
| 12 |
PYTHON3
|
class UnionFindVerSize():
def __init__(self, N):
self._parent = [n for n in range(0, N)]
self._size = [1] * N
self.source = [False] * N
self.group = N
def find_root(self, x):
if self._parent[x] == x: return x
self._parent[x] = self.find_root(self._parent[x])
stack = [x]
while self._parent[stack[-1]]!=stack[-1]:
stack.append(self._parent[stack[-1]])
for v in stack:
self._parent[v] = stack[-1]
return self._parent[x]
def unite(self, x, y):
gx = self.find_root(x)
gy = self.find_root(y)
if gx == gy: return
self.group -= 1
if self._size[gx] < self._size[gy]:
self._parent[gx] = gy
self._size[gy] += self._size[gx]
self.source[gy] |= self.source[gx]
else:
self._parent[gy] = gx
self._size[gx] += self._size[gy]
self.source[gx] |= self.source[gy]
def add_size(self,x):
self.source[self.find_root(x)] = True
def get_size(self, x):
return self._size[self.find_root(x)]
def get_source(self,x):
return self.source[self.find_root(x)]
def is_same_group(self, x, y):
return self.find_root(x) == self.find_root(y)
import sys
input = sys.stdin.buffer.readline
m,n = map(int,input().split())
uf = UnionFindVerSize(n)
S = []
source = []
for i in range(m):
edge = tuple(map(int,input().split()))
if edge[0]==1:
v = edge[1]
if not uf.get_source(v-1):
uf.add_size(v-1)
S.append(i+1)
else:
u,v = edge[1],edge[2]
if not uf.is_same_group(u-1,v-1) and (not uf.get_source(u-1) or not uf.get_source(v-1)):
uf.unite(u-1,v-1)
S.append(i+1)
ans = 1
k = 0
mod = 10**9 + 7
for i in range(n):
if uf.find_root(i)==i:
if uf.get_source(i):
k += uf.get_size(i)
else:
k += uf.get_size(i) - 1
print(pow(2,k,mod),len(S))
S.sort()
print(*S)
| 12 |
PYTHON3
|
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 5e5 + 5;
const int mod = 1e9 + 7;
int f[maxn];
int Find(int u) {
if (u != f[u]) {
f[u] = Find(f[u]);
}
return f[u];
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
f[i] = i;
}
vector<int> V;
for (int i = 1; i <= n; i++) {
int k;
scanf("%d", &k);
if (k == 1) {
int x;
scanf("%d", &x);
int fx = Find(x);
int f0 = Find(0);
if (fx == f0) {
continue;
}
f[fx] = f0;
V.push_back(i);
} else {
int x1, x2;
scanf("%d %d", &x1, &x2);
int fx1 = Find(x1);
int fx2 = Find(x2);
if (fx1 == fx2) {
continue;
}
f[fx1] = fx2;
V.push_back(i);
}
}
long long ans = 1;
for (int i = 1; i <= V.size(); i++) {
ans = ans * 2 % mod;
}
printf("%lld %d\n", ans, V.size());
for (int i = 0; i < V.size(); i++) {
printf("%d ", V[i]);
}
puts("");
}
| 12 |
CPP
|
import sys,io,os
from collections import deque
try:Z=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
except:Z=lambda:sys.stdin.readline().encode()
Y=lambda:map(int,Z().split())
M=10**9+7
n,N=Y()
def path(R):
H=deque();H.append(R)
while P[R]>=0:
R=P[R];H.append(R)
if len(H)>2:P[H.popleft()]=H[-1]
return R
K=[-1]*N;P=[-1]*N;S=[1]*N;R=0;B=[];alr=[0]*N
for i in range(n):
k=*Y(),
if k[0]<2:
a=k[1]-1
if K[a]>=0:
v=path(K[a])
if not alr[v]:alr[v]=1
else:continue
else:K[a]=R;v=R;alr[R]=1;R+=1
B.append(i+1)
continue
a=k[1]-1;b=k[2]-1
if K[a]>=0:
if K[b]>=0:
va,vb=path(K[a]),path(K[b])
if va==vb or (alr[va] and alr[vb]):pass
else:
sa,sb=S[va],S[vb]
if sa>sb:P[vb]=va
else:
P[va]=vb
if sa==sb:S[vb]+=1
B.append(i+1)
if alr[va]:alr[vb]=1
if alr[vb]:alr[va]=1
else:K[b]=path(K[a]);B.append(i+1)
else:
if K[b]>=0:vb=K[a]=path(K[b]);B.append(i+1)
else:K[a]=R;K[b]=R;R+=1;B.append(i+1)
s=len(B)
print(pow(2,s,M),s)
print(' '.join(map(str,B)))
| 12 |
PYTHON3
|
import io
import os
from collections import Counter, defaultdict, deque
MOD = 10 ** 9 + 7
class UnionFind:
def __init__(self, N):
# Union find with component size
# Negative means is a root where value is component size
# Otherwise is index to parent
self.p = [-1 for i in range(N)]
def find(self, i):
# Find root with path compression
if self.p[i] >= 0:
self.p[i] = self.find(self.p[i])
return self.p[i]
else:
return i
def union(self, i, j):
# Union by size
root1 = self.find(j)
root2 = self.find(i)
if root1 == root2:
return
size1 = -self.p[root1]
size2 = -self.p[root2]
if size1 < size2:
self.p[root1] = root2
self.p[root2] = -(size1 + size2)
else:
self.p[root2] = root1
self.p[root1] = -(size1 + size2)
def getComponentSize(self, i):
return -self.p[self.find(i)]
def solve(N, M, vectors):
uf = UnionFind(M + 1)
DUMMY_COMP = M
ans = []
for i, v in enumerate(vectors):
if len(v) == 1:
(a,) = v
if uf.find(a) != uf.find(DUMMY_COMP):
uf.union(a, DUMMY_COMP)
ans.append(i)
else:
assert len(v) == 2
a, b = v
if uf.find(a) != uf.find(b):
uf.union(a, b)
ans.append(i)
for i in range(M):
uf.find(i)
T = pow(2, uf.getComponentSize(DUMMY_COMP) - 1, MOD)
T %= MOD
seen = set()
for i in range(M):
comp = uf.find(i)
if comp != uf.find(DUMMY_COMP) and comp not in seen:
T *= pow(2, uf.getComponentSize(i) - 1, MOD)
T %= MOD
seen.add(comp)
return str(T) + " " + str(len(ans)) + "\n" + " ".join(str(i + 1) for i in ans)
if __name__ == "__main__":
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = 1 # int(input())
for tc in range(1, TC + 1):
(N, M) = [int(x) for x in input().split()]
vectors = [[int(x) - 1 for x in input().split()][1:] for i in range(N)]
ans = solve(N, M, vectors)
print(ans)
| 12 |
PYTHON3
|
/*
Author: AquaBlaze
Time: 2020-12-30 23:16:53
Generated by powerful Codeforces Tool
You can download the binary file in here https://github.com/xalanq/cf-tool (Windows, macOS, Linux)
Keqing best girl :)
Nephren will always be in my heart
*/
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for(int i=(a); i<=(b); ++i)
#define ROF(i, a, b) for(int i=(a); i>=(b); --i)
#define make_unique(a) sort(all((a))), (a).resize(unique(all((a)))-(a).begin())
#define pc(x) putchar(x)
#define MP make_pair
#define MT make_tuple
using namespace std;
typedef long long i64;
typedef tuple<int,int,int> iii;
typedef pair<int,int> pii;
typedef pair<i64,i64> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
const int N = 500005;
vi ans;
vi Stack;
int dsu[N];
int find(int i){
if(i == dsu[i]) return i;
Stack.eb(i);
while(dsu[Stack.back()] != Stack.back()){
Stack.eb(dsu[Stack.back()]);
}
int p = Stack.back();
Stack.pop_back();
while(!Stack.empty()){
dsu[Stack.back()] = p;
Stack.pop_back();
}
return dsu[i];
}
bool occupied[N];
const int inf = (1<<29);
const int mod = 1000000007;
void solve(){
ans.clear();
int n, m;
cin >> n >> m;
FOR(i, 1, m) dsu[i] = i;
FOR(i, 1, n){
int k, a, b;
cin >> k >> a;
if(occupied[a]){
a = find(a);
}
if(k == 1){
if(occupied[a]) continue;
occupied[a] = true;
ans.eb(i);
}else{
cin >> b;
if(occupied[b]) b = find(b);
if(occupied[b]) b = inf;
if(occupied[a]) a = inf;
if(a > b) swap(a,b);
if(a == inf) continue;
if(a == b) continue;
occupied[a] = true;
if(b != inf) dsu[a] = b;
ans.eb(i);
}
}
int cnt = 1;
FOR(i, 1, SZ(ans)) cnt = (cnt+cnt)%mod;
printf("%d %d\n",cnt,SZ(ans));
for(const int &e : ans) printf("%d ",e);
puts("");
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
/*
*
*
*
*
*
*
*
*
*
*
*/
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5+5;
#define pii pair<int,int>
#define ff first
#define ss second
#define pb push_back
struct DisjointSet{
int P[N];
int R[N];
DisjointSet( ){
memset(R,0,sizeof(R));
for( int i = 1 ; i < N ; i ++ ){
P[i] = i;
}
}
int find ( int x ){
if( x == P[x] ){
return x;
}
return P[x] = find( P[x] );
}
void merge ( int x, int y ){
int px = find(x);
int py = find(y);
if( px == py ){
return;
}
if( R[px] > R[py] ){
P[py] = px;
}else{
P[px] = py;
if( R[px] == R[py] ){
R[py]++;
}
}
}
}Ds;
int n, m;
struct edge{
int v, w, ind;
edge( int _v, int _w, int _ind ){
v = _v;
w = _w;
ind = _ind;
}
};
bool cmp( edge a , edge b ){
return a.ind < b.ind;
}
vector <edge> edges;
ll MOD = 1e9+7;
ll fastExpo( ll bas , ll ex ){
if( ex == 0 ) return 1;
ll ret = fastExpo( bas , ex/2ll );
ret = (ret*ret)%MOD;
if( ex%2ll == 1 ) ret = (ret*bas)%MOD;
return ret;
}
int main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m ;
for( int i = 0 ; i < n ; i ++ ){
int k, x, y;
cin >> k ;
if( k == 1 ){
cin >> x ;
y = m+1;
}else if( k == 2 ){
cin >> x >> y ;
}
edges.push_back( edge( x , y , i+1 ) );
}
//sort( edges.begin() , edges.end() , cmp );
ll tot = 0;
set <int> sub;
for( edge e : edges ){
if( Ds.find( e.v ) != Ds.find( e.w ) ){
tot ++;
sub.insert( e.ind );
Ds.merge( e.v , e.w );
}
}
cout << fastExpo( 2 , tot ) << " " << tot << endl ;
for( int x : sub ){
cout << x << " ";
}
cout << endl ;
return 0;
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define int ll
#define fast(); ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define tests() int t; cin>>t; while(t--)
#define endl '\n'
#define F first
#define S second
#define mp make_pair
#define vi vector <ll>
#define pii pair<ll, ll>
#define pdi pair<double, ll>
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define all(x) x.begin(),x.end()
#define f(i,n) for(ll i=0;i<n;i++)
#define shout() {cout << "I'm Here...!!!" << endl;}
#define dbg(x) { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y) { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define dbgv(x) { cout<< #x << ": "; for(auto i : x) cout << i << ' '; cout << '\n'; }
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); }
int fpow(int a, int b) { if (b == 0)return 1; int t = (fpow(a, b / 2)); if (b % 2 == 0)return (t * t); else return ((a) * (t * t)); }
int inf = 1e15 + 100;
int mod = 1e9 + 7;
double pi = 3.1415926;
vi parent;
void make_set(int n) {
f(i, n) parent[i] = i;
}
int find_set(int v) {
if (v == parent[v])
return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b)
parent[b] = a;
}
signed main()
{
int n, m;
cin >> n >> m;
parent.resize(m + 2);
int res = 1;
vi ans;
make_set(m + 1);
f(i, n) {
int c;
cin >> c;
if (c == 1) {
int x;
cin >> x;
if (find_set(x) != find_set(m + 1)) {
union_sets(m + 1, x);
res = (res * 2) % mod;
ans.push_back(i + 1);
}
}
else {
int x, y;
cin >> x >> y;
if (find_set(x) != find_set(y)) {
union_sets(y, x);
res = (res * 2) % mod;
ans.push_back(i + 1);
}
}
}
cout << res << ' ' << ans.size() << endl;
for (int i : ans) cout << i << ' ';
cout << endl;
}
| 12 |
CPP
|
#include<bits/stdc++.h>
using namespace std;
map<int,int>mp;
const int maxn = 5e5 + 10;
typedef long long ll;
const ll mode = 1e9+7;
ll bitcount[65];
ll a[maxn];
int pre[maxn];
int find(int x){
if(x==pre[x]) return x;
else return pre[x] = find(pre[x]);
}
bool unions(int x,int y){
int xx = find(x);
int yy = find(y);
if(xx==yy) return false;
pre[xx]=yy;
return true;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
vector<int>ans;
for(int i=1;i<=m+1;i++) pre[i] = i;
for(int i=1;i<=n;i++){
int a,b=m+1,k;
scanf("%d%d",&k,&a);
if(k>1) scanf("%d",&b);
if(unions(a,b)) ans.push_back(i);
}
int res = 1;
for(int i=0;i<ans.size();i++) res=res*2%mode;
printf("%d %d\n",res,ans.size());
for(auto v:ans){
printf("%d ",v);
}
return 0;
}
// 101
// 110
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
template <int mod> struct Modular {
int value;
Modular(int64_t v = 0) { value = v % mod; if (value < 0) value += mod; }
Modular& operator+=(Modular const& b) { value += b.value; if (value >= mod) value -= mod; return *this; }
Modular& operator-=(Modular const& b) { value -= b.value; if (value < 0) value += mod; return *this; }
Modular& operator*=(Modular const& b) { value = (int64_t) value * b.value % mod; return *this; }
Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
friend Modular power(Modular a, int64_t e) {
Modular res = 1; for (; e; e >>= 1, a *= a) if (e & 1) res *= a;
return res;
}
friend Modular inverse(Modular a) { return power(a, mod - 2); }
friend Modular operator+(Modular a, Modular const b) { return a += b; }
friend Modular operator-(Modular a, Modular const b) { return a -= b; }
friend Modular operator-(Modular const a) { return 0 - a; }
friend Modular operator*(Modular a, Modular const b) { return a *= b; }
friend Modular operator/(Modular a, Modular const b) { return a /= b; }
friend bool operator==(Modular const& a, Modular const& b) { return a.value == b.value; }
friend bool operator!=(Modular const& a, Modular const& b) { return a.value != b.value; }
friend ostream& operator<<(ostream& os, Modular const& a) { return os << a.value; }
};
using Mint = Modular<int(1e9) + 7>;
struct DSU {
vector<int> p;
DSU(int n) : p(n) {
iota(p.begin(), p.end(), 0);
}
int find(int i) {
if (i == p[i]) return i;
return p[i] = find(p[i]);
}
bool merge(int x, int y) {
x = find(x), y = find(y);
if (x == y) return false;
p[x] = y;
return true;
}
};
int main() {
int n, m;
cin >> n >> m;
Mint ans = 1;
vector<int> a;
DSU dsu(m + 2);
for (int i = 1; i <= n; i++) {
int k;
cin >> k;
if (k == 1) {
int x;
cin >> x;
if (dsu.merge(x, m + 1)) {
a.push_back(i);
ans *= 2;
}
} else {
int x, y;
cin >> x >> y;
if (dsu.merge(x, y)) {
a.push_back(i);
ans *= 2;
}
}
}
cout << ans << ' ' << a.size() << '\n';
for (int i = 0; i < a.size(); i++) {
cout << a[i] << " \n"[i == a.size() - 1];
}
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
#define taskname "TEST"
typedef long long ll;
typedef long double ld;
const int N = 5e5 + 5;
const int MOD = 1e9 + 7;
int n, m, up[N];
vector <int> res;
void init() {
for (int i = 1; i <= 5e5 + 1; i++) up[i] = i;
}
int findset(int u) {
if (up[u] != u) up[u] = findset(up[u]);
return up[u];
}
bool bunion(int u, int v) {
u = findset(u);
v = findset(v);
up[u] = v;
return (u != v);
}
void inp() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int k, u, v = m + 1; cin >> k;
cin >> u;
if (k > 1) cin >> v;
if (bunion(u, v)) res.push_back(i);
}
}
void out() {
int ans = 1;
for (int i = 0; i < res.size(); i++) ans = ((ll)2*ans)%MOD;
cout << ans << " " << res.size() << "\n";
for (int i = 0; i < res.size(); i++) cout << res[i] << " ";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen(taskname".INP", "r", stdin);
// freopen(taskname".OUT", "w", stdout);
init();
inp();
out();
}
| 12 |
CPP
|
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
def __init__(self, file):
self.newlines = 0
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline()
# --------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().split())
def RLL(): return list(map(int, sys.stdin.readline().split()))
def N(): return int(input())
def S(): return input().strip()
def print_list(l): print(' '.join(map(str, l)))
# sys.setrecursionlimit(100000)
# import random
# from functools import reduce
# from functools import lru_cache
# from heapq import *
# from collections import deque as dq
# from math import gcd
# import bisect as bs
# from collections import Counter
# from collections import defaultdict as dc
def find(region, u):
path = []
while region[u] != u:
path.append(u)
u = region[u]
for v in path:
region[v] = u
return u
def union(region, u, v):
u, v = find(region, u), find(region, v)
region[u] = v
return u != v
M = 10 ** 9 + 7
n, m = RL()
region = list(range(m + 1))
ans = []
for i in range(1, n + 1):
s = RLL()
t = s[2] if s[0] == 2 else 0
if union(region, s[1], t):
ans.append(i)
res = 1
for _ in range(len(ans)):
res = (res << 1) % M
print(res, len(ans))
print_list(ans)
| 12 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef LOCAL
#define debug(...) {\
std::cout << "[" << __FUNCTION__ << ":" << __LINE__ << "] " << #__VA_ARGS__ << " " << __VA_ARGS__ << std::endl;\
}
#else
#define debug(...)
#endif
const long long mod = 1000000007;
#define MOD(x) ((x)%mod)
struct UnionFind {
vector<int> par;
vector<int> siz;
vector<int> val;
UnionFind(int N) {
par.resize(N);
siz.resize(N);
val.resize(N);
for (int i = 0; i < N; i++) {
par[i] = i;
siz[i] = 1;
val[i] = 0;
}
}
int& operator[](const int &x) {
return val[getParent(x)];
}
int getParent(int x) {
int t = x;
while (par[x] != x) {
x = par[x];
}
par[t] = x;
return x;
}
int getSize(const int &x) {
return siz[getParent(x)];
}
bool merge(int x, int y) {
x = getParent(x);
y = getParent(y);
if (x == y) {
return false;
}
if (val[x] and val[y]) {
return false;
}
if (siz[x] > siz[y]) {
swap(x, y); // siz[x] is smaller
}
par[x] = y;
siz[y] += siz[x];
val[y] = val[x] or val[y];
return true;
}
bool connected(const int &x, const int &y) {
return getParent(x) == getParent(y);
}
};
long long power(long long x, long long n) {
int i = 0;
long long d = x;
long long ats = 1;
while (n > 0) {
if (n & (1ll << i)) {
n ^= (1ll << i);
ats *= d;
ats %= mod;
}
i++;
d = (d * d) % mod;
}
return ats;
}
/*input
3 2
1 1
1 2
2 2 1
*/
/*input
2 3
2 1 3
2 1 2
*/
/*input
3 5
2 1 2
1 3
1 4
*/
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long n, m;
cin >> n >> m;
UnionFind par(m);
set<int> imt;
for (int i = 0; i < n; ++i) {
int k;
cin >> k;
if (k == 1) {
int a;
cin >> a;
a--;
if (!par[a]) {
par[a] = true;
imt.insert(i);
}
}
else if (k == 2) {
int a, b;
cin >> a >> b;
a--, b--;
if (par.merge(a, b)) {
imt.insert(i);
}
}
}
long long vis = 0;
for (int i = 0; i < m; ++i) {
if (par.getParent(i) == i) {
if (par.val[i]) {
vis += par.siz[i];
}
else {
vis += par.siz[i] - 1;
}
}
}
cout << power(2, vis) << " " << imt.size() << "\n";
for (auto &&i : imt) {
cout << i + 1 << " ";
}
}
| 12 |
CPP
|
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
def find(self, a):
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a:
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b):
self.parent[self.find(b)] = self.find(a)
def main():
m,n = map(int,input().split())
ans = 0
ans2 = []
color = UnionFind(n + 1)
for i in range(m):
t = list(map(int,input().split()))
if t[0] == 1:
if color.find(n) != color.find(t[1] - 1):
ans2.append(i + 1)
color.union(n, t[1] - 1)
else:
if color.find(t[2] - 1) != color.find(t[1] - 1):
ans2.append(i + 1)
color.union(t[2] - 1, t[1] - 1)
print(str(pow(2,len(ans2), 10 ** 9 + 7)) + " " + str(len(ans2)))
print(" ".join(map(str,ans2)))
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
| 12 |
PYTHON3
|
#include <bits/stdc++.h>
//define/typeDef
#define all(a) a.begin(), a.end()
#define double long double
#define int long long
#define NIL 0
#define INF LLONG_MAX
#define loop(n) for(int i=0;i<n; i++)
#define rloop(n) for(int i=n-1; i>=0; i--)
using namespace std;
const int mod = 1e9 + 7;
const int N = 5e5 + 50;
int par[N], sze[N];
int getParent(int node) {
while (par[node] != node) {
par[node] = par[par[node]];
node = par[node];
}
return node;
}
bool makeUnion(pair<int, int> edge) {
int u = edge.first;
int v = edge.second;
u = getParent(u);
v = getParent(v);
if (u == v)
return false;
if (sze[u] < sze[v])
swap(u, v);
sze[u] += sze[v];
par[v] = par[u];
return true;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int m;
cin >> m;
vector<pair<int, int> > edges;
for (int i = 0; i < n; i++) {
int cnt;
cin >> cnt;
int u, v = m + 1;
cin >> u;
if (cnt > 1)
cin >> v;
edges.emplace_back(u, v);
}
int mst = 1;
for (int i = 1; i <= m + 1; i++) {
par[i] = i, sze[i] = 1;
}
vector<int> ans;
int cnt = 0;
for (auto edge: edges) {
if (makeUnion(edge)) {
mst = (mst * 2) % mod;
ans.push_back(++cnt);
} else {
++cnt;
}
}
cout << mst << ' ';
cout<<ans.size()<<'\n';
for(auto x: ans){
cout<<x<<' ';
}
}
| 12 |
CPP
|
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5 + 5;
const int mod = 1e9 + 7;
int n, m, cnt = 1, par[N];
vector<int> ans;
int find(int u) {
return (u == par[u]) ? u : par[u] = find(par[u]);
}
bool join(int a, int b) {
a = find(a), b = find(b);
if(a == b)
return 0;
par[b] = a;
return 1;
}
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 1;i <= m + 1;i++)
par[i] = i;
for(int i = 1;i <= n;i++) {
int k, a, b = m + 1;
cin >> k >> a;
if(k > 1)
cin >> b;
if(join(a, b))
ans.push_back(i), cnt = (2ll * cnt) % mod;
}
cout << cnt << " " << ans.size() << '\n';
for(auto &i : ans)
cout << i << " ";
return 0;
}
| 12 |
CPP
|
import sys
input = iter(sys.stdin.read().splitlines()).__next__
class UnionFind: # based on submission 102831279
def __init__(self, n):
""" elements are 0, 1, 2, ..., n-1 """
self.parent = list(range(n))
def find(self, x):
found = x
while self.parent[found] != found:
found = self.parent[found]
while x != found:
y = self.parent[x]
self.parent[x] = found
x = y
return found
def union(self, x, y):
self.parent[self.find(x)] = self.find(y)
n, m = map(int, input().split())
S_prime = []
# redundant (linearly dependent) vectors would be part of cycle
uf = UnionFind(m+1)
for index in range(1, n+1):
# one-hot vectors become (x_1, m)
vector_description = [int(i)-1 for i in input().split()] + [m]
u, v = vector_description[1:3]
if uf.find(u) == uf.find(v):
continue
S_prime.append(index)
uf.union(u, v)
# 2**|S'| different sums among |S'| linearly independent vectors
T_size = pow(2, len(S_prime), 10**9+7)
print(T_size, len(S_prime))
# print(S_prime)
print(*sorted(S_prime))
| 12 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using pi=pair<int,int>;
using vi=vc<int>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.a<<","<<p.b<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ";
dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif
using uint=unsigned;
using ull=unsigned long long;
template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
return os<<vc<t>(all(a));
}
template<int i,class T>
void print_tuple(ostream&,const T&){
}
template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
if(i)os<<",";
os<<get<i>(t);
print_tuple<i+1,T,Args...>(os,t);
}
template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
os<<"{";
print_tuple<0,tuple<Args...>,Args...>(os,t);
return os<<"}";
}
template<class t>
void print(t x,int suc=1){
cout<<x;
if(suc==1)
cout<<"\n";
if(suc==2)
cout<<" ";
}
ll read(){
ll i;
cin>>i;
return i;
}
vi readvi(int n,int off=0){
vi v(n);
rep(i,n)v[i]=read()+off;
return v;
}
pi readpi(int off=0){
int a,b;cin>>a>>b;
return pi(a+off,b+off);
}
template<class t,class u>
void print(const pair<t,u>&p,int suc=1){
print(p.a,2);
print(p.b,suc);
}
template<class T>
void print(const vector<T>&v,int suc=1){
rep(i,v.size())
print(v[i],i==int(v.size())-1?suc:2);
}
string readString(){
string s;
cin>>s;
return s;
}
template<class T>
T sq(const T& t){
return t*t;
}
//#define CAPITAL
void yes(bool ex=true){
#ifdef CAPITAL
cout<<"YES"<<"\n";
#else
cout<<"Yes"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void no(bool ex=true){
#ifdef CAPITAL
cout<<"NO"<<"\n";
#else
cout<<"No"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void possible(bool ex=true){
#ifdef CAPITAL
cout<<"POSSIBLE"<<"\n";
#else
cout<<"Possible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void impossible(bool ex=true){
#ifdef CAPITAL
cout<<"IMPOSSIBLE"<<"\n";
#else
cout<<"Impossible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
constexpr ll ten(int n){
return n==0?1:ten(n-1)*10;
}
const ll infLL=LLONG_MAX/3;
#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif
int topbit(signed t){
return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
return __builtin_popcount(t);
}
int popcount(ll t){
return __builtin_popcountll(t);
}
bool ispow2(int i){
return i&&(i&-i)==i;
}
ll mask(int i){
return (ll(1)<<i)-1;
}
bool inc(int a,int b,int c){
return a<=b&&b<=c;
}
template<class t> void mkuni(vc<t>&v){
sort(all(v));
v.erase(unique(all(v)),v.ed);
}
ll rand_int(ll l, ll r) { //[l, r]
#ifdef LOCAL
static mt19937_64 gen;
#else
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif
return uniform_int_distribution<ll>(l, r)(gen);
}
template<class t>
void myshuffle(vc<t>&a){
rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}
template<class t>
int lwb(const vc<t>&v,const t&a){
return lower_bound(all(v),a)-v.bg;
}
vvc<int> readGraph(int n,int m){
vvc<int> g(n);
rep(i,m){
int a,b;
cin>>a>>b;
//sc.read(a,b);
a--;b--;
g[a].pb(b);
g[b].pb(a);
}
return g;
}
vvc<int> readTree(int n){
return readGraph(n,n-1);
}
//VERIFY: yosupo
//KUPC2017J
//AOJDSL1A
//without rank
struct unionfind{
vi p,s;
int c;
unionfind(int n):p(n,-1),s(n,1),c(n){}
int find(int a){
return p[a]==-1?a:(p[a]=find(p[a]));
}
//set b to a child of a
bool unite(int a,int b){
a=find(a);
b=find(b);
if(a==b)return false;
p[b]=a;
s[a]+=s[b];
c--;
return true;
}
bool same(int a,int b){
return find(a)==find(b);
}
int sz(int a){
return s[find(a)];
}
};
//mint107 は verify してねえ
//#define DYNAMIC_MOD
struct modinfo{uint mod,root;
#ifdef DYNAMIC_MOD
constexpr modinfo(uint m,uint r):mod(m),root(r),im(0){set_mod(m);}
ull im;
constexpr void set_mod(uint m){
mod=m;
im=ull(-1)/m+1;
}
uint product(uint a,uint b)const{
ull z=ull(a)*b;
uint x=((unsigned __int128)z*im)>>64;
uint v=uint(z)-x*mod;
return v<mod?v:v+mod;
}
#endif
};
template<modinfo const&ref>
struct modular{
static constexpr uint const &mod=ref.mod;
static modular root(){return modular(ref.root);}
uint v;
//modular(initializer_list<uint>ls):v(*ls.bg){}
modular(ll vv=0){s(vv%mod+mod);}
modular& s(uint vv){
v=vv<mod?vv:vv-mod;
return *this;
}
modular operator-()const{return modular()-*this;}
modular& operator+=(const modular&rhs){return s(v+rhs.v);}
modular&operator-=(const modular&rhs){return s(v+mod-rhs.v);}
modular&operator*=(const modular&rhs){
#ifndef DYNAMIC_MOD
v=ull(v)*rhs.v%mod;
#else
v=ref.product(v,rhs.v);
#endif
return *this;
}
modular&operator/=(const modular&rhs){return *this*=rhs.inv();}
modular operator+(const modular&rhs)const{return modular(*this)+=rhs;}
modular operator-(const modular&rhs)const{return modular(*this)-=rhs;}
modular operator*(const modular&rhs)const{return modular(*this)*=rhs;}
modular operator/(const modular&rhs)const{return modular(*this)/=rhs;}
modular pow(int n)const{
modular res(1),x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n>>=1;
}
return res;
}
modular inv()const{return pow(mod-2);}
/*modular inv()const{
int x,y;
int g=extgcd<ll>(v,mod,x,y);
assert(g==1);
if(x<0)x+=mod;
return modular(x);
}*/
friend modular operator+(int x,const modular&y){
return modular(x)+y;
}
friend modular operator-(int x,const modular&y){
return modular(x)-y;
}
friend modular operator*(int x,const modular&y){
return modular(x)*y;
}
friend modular operator/(int x,const modular&y){
return modular(x)/y;
}
friend ostream& operator<<(ostream&os,const modular&m){
return os<<m.v;
}
friend istream& operator>>(istream&is,modular&m){
ll x;is>>x;
m=modular(x);
return is;
}
bool operator<(const modular&r)const{return v<r.v;}
bool operator==(const modular&r)const{return v==r.v;}
bool operator!=(const modular&r)const{return v!=r.v;}
explicit operator bool()const{
return v;
}
};
#ifndef DYNAMIC_MOD
//extern constexpr modinfo base{998244353,3};
extern constexpr modinfo base{1000000007,0};
//modinfo base{1,0};
#else
modinfo base(1,0);
extern constexpr modinfo base107(1000000007,0);
using mint107=modular<base107>;
#endif
using mint=modular<base>;
void slv(){
int m,n;cin>>m>>n;
unionfind uf(n);
vi has(n);
vi ans;
rep(idx,m){
int k;cin>>k;
if(k==1){
int x;cin>>x;x--;
x=uf.find(x);
if(!has[x]){
ans.pb(idx);
has[x]=1;
}
}else{
int x,y;cin>>x>>y;
x=uf.find(x-1);
y=uf.find(y-1);
if(x!=y){
if(!has[x]||!has[y]){
has[x]|=has[y];
uf.unite(x,y);
ans.pb(idx);
}
}
}
}
print(mint(2).pow(si(ans)),2);
print(si(ans));
for(auto&v:ans)v++;
print(ans);
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
//int t;cin>>t;rep(_,t)
slv();
}
| 12 |
CPP
|
"""
Author - Satwik Tiwari .
19th Jan , 2021 - Tuesday
"""
#===============================================================================================
#importing some useful libraries.
from __future__ import division, print_function
# from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
# from itertools import *
from heapq import *
from math import gcd, factorial,floor,ceil,sqrt,log2
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
#==============================================================================================
#fast I/O region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
# inp = lambda: sys.stdin.readline().rstrip("\r\n")
#===============================================================================================
### START ITERATE RECURSION ###
from types import GeneratorType
def iterative(f, stack=[]):
def wrapped_func(*args, **kwargs):
if stack: return f(*args, **kwargs)
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
continue
stack.pop()
if not stack: break
to = stack[-1].send(to)
return to
return wrapped_func
#### END ITERATE RECURSION ####
#===============================================================================================
#some shortcuts
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var)) #for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
# def graph(vertex): return [[] for i in range(0,vertex+1)]
def testcase(t):
for pp in range(t):
solve(pp)
def google(p):
print('Case #'+str(p)+': ',end='')
def lcm(a,b): return (a*b)//gcd(a,b)
def power(x, y, p) :
y%=(p-1) #not so sure about this. used when y>p-1. if p is prime.
res = 1 # Initialize result
x = x % p # Update x if it is more , than or equal to p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) : # If y is odd, multiply, x with result
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
def ncr(n,r): return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))
def isPrime(n) :
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
inf = pow(10,21)
mod = 10**9+7
#===============================================================================================
# code here ;))
def bucketsort(order, seq):
buckets = [0] * (max(seq) + 1)
for x in seq:
buckets[x] += 1
for i in range(len(buckets) - 1):
buckets[i + 1] += buckets[i]
new_order = [-1] * len(seq)
for i in reversed(order):
x = seq[i]
idx = buckets[x] = buckets[x] - 1
new_order[idx] = i
return new_order
def ordersort(order, seq, reverse=False):
bit = max(seq).bit_length() >> 1
mask = (1 << bit) - 1
order = bucketsort(order, [x & mask for x in seq])
order = bucketsort(order, [x >> bit for x in seq])
if reverse:
order.reverse()
return order
def long_ordersort(order, seq):
order = ordersort(order, [int(i & 0x7fffffff) for i in seq])
return ordersort(order, [int(i >> 31) for i in seq])
def multikey_ordersort(order, *seqs, sort=ordersort):
for i in reversed(range(len(seqs))):
order = sort(order, seqs[i])
return order
class DisjointSetUnion:
def __init__(self, n):
self.parent = list(range(n))
self.size = [1] * n
self.num_sets = n
def find(self, a):
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a:
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b):
a, b = self.find(a), self.find(b)
if a != b:
if self.size[a] < self.size[b]:
a, b = b, a
self.num_sets -= 1
self.parent[b] = a
self.size[a] += self.size[b]
def set_size(self, a):
return self.size[self.find(a)]
def __len__(self):
return self.num_sets
def solve(case):
n,m = sep()
dsu = DisjointSetUnion(m+1)
# l = []
# r = []
# ind = []
# for i in range(n):
# a = lis()
# if(a[0] == 1):
# l.append(0)
# r.append(a[1])
# ind.append(i)
# else:
# # if(a[1] > a[2]): a[1],a[2] = a[2],a[1]
# l.append(a[1])
# r.append(a[2])
# ind.append(i)
#
# order = multikey_ordersort(range(len(l)),l,r,ind)
# sl = []
# for i in order:
# sl.append((l[i],r[i],ind[i]))
take = [1]*n
for index in range(n):
a = lis()
if(a[0] == 1):
i,j = 0,a[1]
else:
i,j = a[1],a[2]
# print(i,j)
grp1 = dsu.find(i)
grp2 = dsu.find(j)
if(grp1 == grp2):
take[index] = 0
else:
dsu.union(i,j)
ans = []
for i in range(n):
if(take[i]):
ans.append(i)
ans = sorted(ans)
print(power(2,len(ans),mod),len(ans))
print(' '.join(str(i+1) for i in ans))
#lexicographically based on the order they occur in input. no need to sort, we can directly look for cycles.
testcase(1)
# testcase(int(inp()))
| 12 |
PYTHON3
|
from collections import defaultdict
MOD=10**9+7
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
def find(self, a): #return parent of a. a and b are in same set if they have same parent
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a: #path compression
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b): #union a and b
self.parent[self.find(b)] = self.find(a)
def oneLineArrayPrint(arr):
print(' '.join([str(x+1) for x in arr]))
def canTake(newEdgesCnt,newVerticesCnt,newHasOne):
if newHasOne:return newEdgesCnt<=newVerticesCnt
else: return newEdgesCnt<=newVerticesCnt-1
def handleOne(i,v1,sPrime,cEc,cVc,cHO,vV,uf):
parent=uf.find(v1)
oldEdgesCnt=cEc[parent]
oldVerticesCnt=cVc[parent]
newEdgesCnt=oldEdgesCnt+1
if vV[v1]==False:newVerticesCnt=oldVerticesCnt+1
else:newVerticesCnt=oldVerticesCnt
# print('i:{} oldE:{} oldV:{} newE:{} newV:{}'.format(i,oldEdgesCnt,oldVerticesCnt,newEdgesCnt,newVerticesCnt))
if canTake(newEdgesCnt,newVerticesCnt,True):
sPrime.append(i)
vV[v1]=True
cEc[parent]=newEdgesCnt
cVc[parent]=newVerticesCnt
cHO[parent]=True
return
def handleTwo(i,v1,v2,sPrime,cEc,cVc,cHO,vV,uf):
parent1=uf.find(v1)
parent2=uf.find(v2)
if parent1!=parent2:
oldEdgesCnt=cEc[parent1]+cEc[parent2]
oldVerticesCnt=cVc[parent1]+cVc[parent2]
else:
oldEdgesCnt=cEc[parent1]
oldVerticesCnt=cVc[parent1]
newEdgesCnt=oldEdgesCnt+1
newVerticesCnt=oldVerticesCnt
if vV[v1]==False:newVerticesCnt+=1
if vV[v2]==False:newVerticesCnt+=1
if canTake(newEdgesCnt,newVerticesCnt,cHO[parent1] or cHO[parent2]):
sPrime.append(i)
vV[v1]=True
vV[v2]=True
uf.union(v1,v2)
newParent=uf.find(v1)
cEc[newParent]=newEdgesCnt
cVc[newParent]=newVerticesCnt
cHO[newParent]=cHO[parent1] or cHO[parent2]
return
def solveActual():
uf=UnionFind(m)
#each component has k vertices.
#each component shall have at most k-1 useful edges if componentHasOne==False, or k edges if it's True
#pick the k-1 or k edges with the smallest values
#each component contributes independently 2**(nEdges) to the number of ways.
#just find 2**(nEdges or number of included indexes)
sPrime=[]
cEc=[0 for _ in range(m)] #cEc[parent]=component edge counts
cVc=[0 for _ in range(m)] #cVc[parent]=component vertex count
cHO=[False for _ in range(m)] #countHasOne cHO[parent]=True if this component has an edge with only 1 vertex
vV=[False for _ in range(m)] #True if the vertex has been visited
for i,x in enumerate(vS): #idx,vertex(vertices)
nVertices=len(x)
if nVertices==1:
handleOne(i,x[0],sPrime,cEc,cVc,cHO,vV,uf)
else:
handleTwo(i,x[0],x[1],sPrime,cEc,cVc,cHO,vV,uf)
TSize=pow(2,len(sPrime),MOD)
print('{} {}'.format(TSize,len(sPrime)))
oneLineArrayPrint(sPrime)
n,m=[int(x) for x in input().split()]
vS=[] #0-indexed
for _ in range(n):
xx=[int(x) for x in input().split()]
#number of 1s, coordinates with 1s
for i in range(1,len(xx)):
xx[i]-=1 #0-index
vS.append(xx[1:])
solveActual()
| 12 |
PYTHON3
|
#include<bits/stdc++.h>
#define it register int
#define ct const int
#define il inline
using namespace std;
typedef long long ll;
#define rll register ll
#define cll const ll
#define mkp make_pair
#define fir first
#define sec second
const int N=1000005;
#define P 1000000007
template<class I>
il I Min(I p,I q){return p<q?p:q;}
template<class I>
il void ckMin(I&p,I q){p=(p<q?p:q);}
int T,n,cn[N],u,v,ans,fa[N],m;
bool tag[N];
vector<int> o;
il int fd(ct x){return fa[x]^x?fa[x]=fd(fa[x]):x;}
il void mer(it u,it v,ct i){u=fd(u),v=fd(v),(u^v)&&(tag[u]+tag[v]<=1)?fa[u]=v,tag[v]|=tag[u],o.push_back(i),(ans<<=1,ans>=P?ans-=P:0):0;}
int main(){
scanf("%d%d",&n,&m);it i,op;ans=1;
for(i=1;i<=m;++i) fa[i]=i;
for(i=1;i<=n;++i) scanf("%d%d",&op,&u),op^1?scanf("%d",&v),mer(u,v,i),0:(u=fd(u),!tag[u]?o.push_back(i),ans<<=1,ans>=P?ans-=P:0,tag[u]=1:0);
printf("%d %d\n",ans,o.size());
for(const int &i : o) printf("%d ",i);
return 0;
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " = " << x << endl
#define _ << " _ " <<
#define fi first
#define se second
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
vi p;
int find_p(int x) {
if (x == p[x]) return x;
return p[x] = find_p(p[x]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
srand(time(0));
int n, m;
cin >> n >> m;
vi sol;
p.resize(m + 1);
iota(p.begin(), p.end(), 0);
for (int i = 0; i < n; i++) {
int k, x, y;
cin >> k >> x;
if (k == 2) cin >> y;
else y = 0;
x = find_p(x);
y = find_p(y);
if (x == y) continue;
if (rand() % 2) swap(x, y);
p[x] = y;
sol.push_back(i + 1);
}
const int MOD = 1e9 + 7;
int cnt = sol.size(), sz = 1;
for (int i = 0; i < cnt; i++) {
sz += sz;
if (sz >= MOD) sz -= MOD;
}
cout << sz << ' ' << cnt << '\n';
for (auto it : sol) cout << it << ' ';
cout << '\n';
return 0;
}
| 12 |
CPP
|
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
def find(self, a): #return parent of a. a and b are in same set if they have same parent
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a: #path compression
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b): #union a and b
self.parent[self.find(b)] = self.find(a)
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
#######
n,m=[int(x) for x in input().split()]
uf=UnionFind(m+1)
hasOne=[False for _ in range(m+1)] #hasOne[parent]
#a tree cannot have cycles
#a tree can have at most vector with 1 "1"
sPrime=[]
for i in range(n):
inp=[int(x) for x in input().split()]
if inp[0]==1:#vector has 1 "1"
parent=uf.find(inp[1])
if hasOne[parent]==False:#take this vector
sPrime.append(i+1)
hasOne[parent]=True
else:
parent1,parent2=uf.find(inp[1]),uf.find(inp[2])
if parent1!=parent2: #no cycle. will join 2 trees
if not (hasOne[parent1] and hasOne[parent2]):#take this vector
sPrime.append(i+1)
uf.union(inp[1],inp[2])
newParent=uf.find(inp[1])
hasOne[newParent]=hasOne[parent1] or hasOne[parent2]
S_magnitude=len(sPrime)
T_magnitude=pow(2,S_magnitude,10**9+7)
print('{} {}'.format(T_magnitude,S_magnitude))
oneLineArrayPrint(sPrime)
| 12 |
PYTHON3
|
#include <iostream>
#include <vector>
#define ll long long
#define f first
#define s second
#define NMAX 500000
#define MOD 1000000007
using namespace std;
ll n, m, viz[NMAX+10];
ll nr, ans = 1, ans2, tata[NMAX+10], rang[NMAX+10];
vector <ll> nod[NMAX+10], a;
pair <ll, ll> v[NMAX+10];
ll findDaddy(ll x)
{ ll y = x, r = x;
while(r != tata[r]) r = tata[r];
while(x != tata[x])
{ y = tata[y];
tata[x] = r;
x = y;
}
return r;
}
void unite(ll x, ll y)
{ if(rang[x] < rang[y]) tata[x] = y;
else tata[y] = x;
if(rang[x] == rang[y]) rang[x]++;
}
ll lgput(ll a, ll n)
{ if(!n) return 1;
if(n % 2 == 0) return lgput(a*a % MOD, n/2);
return a * lgput(a*a % MOD, n/2) % MOD;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(ll i=1; i<=n; i++)
{ ll k;
cin >> k;
if(k == 1)
{ cin >> v[i].f;
ll x = v[i].f;
nod[0].push_back(x);
nod[x].push_back(0);
}
else
{ cin >> v[i].f >> v[i].s;
ll x = v[i].f, y = v[i].s;
nod[x].push_back(y);
nod[y].push_back(x);
}
}
for(ll i=0; i<=m; i++) tata[i] = i, rang[i] = 1;
for(ll i=1; i<=n; i++)
{ ll val1, val2;
if(!v[i].s) val1 = findDaddy(v[i].f), val2 = findDaddy(0);
else val1 = findDaddy(v[i].f), val2 = findDaddy(v[i].s);
if(val1 == val2) continue;
ans2++;
a.push_back(i);
unite(val1, val2);
}
cout << lgput(2LL, ans2) << ' ' << ans2 << '\n';
for(ll u : a) cout << u << ' ';
cout << '\n';
return 0;
}
| 12 |
CPP
|
import sys
input = sys.stdin.readline
MOD = 10 ** 9 + 7
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
dic = {r:[] for r in self.roots()}
for i in range(self.n):
dic[self.find(i)].append(i)
return dic
def __str__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
def main():
n, m = map(int, input().split())
ans = []
UF = UnionFind(m + 1)
for i in range(1, n + 1):
q = list(map(int, input().split()))
if q[0] == 1:
x = 0
y = q[1]
else:
x = q[1]
y = q[2]
if not UF.same(x, y):
ans.append(i)
UF.union(x, y)
cnt = 0
for x in UF.roots():
cnt += UF.size(x) - 1
t = pow(2, cnt, MOD)
print(t, len(ans))
print(*ans)
for _ in range(1):
main()
| 12 |
PYTHON3
|
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#pragma region Macros
#define FOR(i, l, r) for(int i = (l) ;i < (r) ;i++)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n+1)
#define RFOR(i, l, r) for(int i = (l) ; i >= (r) ; i--)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n , 1)
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = int>
using V = vector<T>;
template <class T = int>
using VV = V<V<T>>;
#define ll long long
using ld = long double;
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
#define VEC(type, name, size) \
V<type> name(size); \
INPUT(name)
#define VVEC(type, name, h, w) \
VV<type> name(h, V<type>(w)); \
INPUT(name)
#define INT(...) \
int __VA_ARGS__; \
INPUT(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
INPUT(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
INPUT(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
INPUT(__VA_ARGS__)
#define DOUBLE(...) \
DOUBLE __VA_ARGS__; \
INPUT(__VA_ARGS__)
#define LD(...) \
LD __VA_ARGS__; \
INPUT(__VA_ARGS__)
void scan(int &a) { cin >> a;}
void scan(long long &a) { cin >> a;}
void scan(char &a) { cin >> a;}
void scan(double &a) { cin >> a;}
void scan(long double &a) { cin >> a;}
void scan(char a[]) { scanf("%s", a);}
void scan(string &a) { cin >> a;}
template <class T>
void scan(V<T> &);
template <class T, class L>
void scan(pair<T, L> &);
template <class T>
void scan(V<T> &a){
for(auto &i : a) scan(i);
}
template <class T, class L>
void scan(pair<T, L> &p){
scan(p.first);
scan(p.second);
}
template <class T>
void scan(T &a) { cin >> a;}
void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail){
scan(head);
INPUT(tail...);
}
template <class T>
inline void print(T x) { cout << x << '\n'; }
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for(T &in : v) is >> in;
return is;
}
template <class T>
ostream &operator<<(ostream &os, const V<T> &v) {
REP(i, SZ(v)) {
if(i) os << " ";
os << v[i];
}
return os;
}
//debug
template <typename T>
void view(const V<T> &v) {
cerr << "{ ";
for(const auto &e : v) {
cerr << e << ", ";
}
cerr << "\b\b }";
}
template <typename T>
void view(const VV<T> &vv) {
cerr << "{\n";
for(const auto &v : vv) {
cerr << "\t";
view(v);
cerr << "\n";
}
cerr << "}";
}
template <typename T, typename U>
void view(const V<pair<T, U>> &v) {
cerr << "{\n";
for(const auto &c : v) cerr << "\t(" << c.first << ", " << c.second << ")\n";
cerr << "}";
}
template <typename T, typename U>
void view(const map<T, U> &m) {
cerr << "{\n";
for(auto &t : m) cerr << "\t[" << t.first << "] : " << t.second << "\n";
cerr << "}";
}
template <typename T, typename U>
void view(const pair<T, U> &p) { cerr << "(" << p.first << ", " << p.second << ")"; }
template <typename T>
void view(const set<T> &s) {
cerr << "{ ";
for(auto &t : s) {
view(t);
cerr << ", ";
}
cerr << "\b\b }";
}
template <typename T>
void view(T e) { cerr << e; }
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
view(H);
cerr << ", ";
debug_out(T...);
}
#define debug(...) \
do { \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
debug_out(__VA_ARGS__); \
cerr << "\b\b]\n"; \
} while(0)
#else
#define debug(...) (void(0))
#endif
template <class T>
V<T> press(V<T> &x) {
V<T> res = x;
sort(all(res));
res.erase(unique(all(res)), res.end());
REP(i, SZ(x)) {
x[i] = lower_bound(all(res), x[i]) - res.begin();
}
return res;
}
template <class T>
inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
if(b) {
cout << -1 << '\n';
exit(0);
}
}
template <class T>
inline void fin(bool b = true, T e = 0) {
if(b) {
cout << e << '\n';
exit(0);
}
}
template <class T>
T divup(T x, T y) { return (x + (y - 1)) / y; }
template <typename T>
T pow(T a, long long n, T e = 1) {
T ret = e;
while(n) {
if(n & 1) ret *= a;
a *= a;
n >>= 1;
}
return ret;
}
struct iofast {
iofast() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
}
} iofast_;
const int inf = 1e9;
const ll INF = 1e18;
#pragma endregion
inline int topbit(unsigned long long x){
return x?63-__builtin_clzll(x):-1;
}
inline int popcount(unsigned long long x){
return __builtin_popcountll(x);
}
inline int parity(unsigned long long x){//popcount%2
return __builtin_parity(x);
}
class DisjointSet{
public:
vector<ll> rank,p,sz;
DisjointSet(){}
DisjointSet(ll size){ //作られうる木の頂点数の最大値を入れる。
rank.resize(size,0);
p.resize(size,0);
sz.resize(size,1);
REP(i,size) makeSet(i);
}
void makeSet(ll x){ //xだけが属する木を作る。
p[x]=x;
rank[x]=0;
}
bool same(ll x,ll y){ //xとyが同じ木に属するかどうか
return findSet(x)==findSet(y);
}
void unite(ll x, ll y){
link(findSet(x),findSet(y));
}
void link(ll x, ll y){ //rankが大きい方の根に小さい方の根をつける。
if(rank[x]>rank[y]){
p[y]=x;
}
else{
p[x]=y;
if(rank[x]==rank[y]){
rank[y]++; //xとyの木のrankが同じであれば、統合するとrankが1増える。
}
}
sz[x]+=sz[y];
sz[y]=sz[x];
}
ll findSet(ll x){ //xが属する木の根の番号を返す
if(x!=p[x]){
p[x]=findSet(p[x]);
}
return p[x];
}
};
const ll mod=1e9+7;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
vector<mint> fact, ifact;
combination(ll n):fact(n+1),ifact(n+1) {
assert(n < mod);
fact[0] = 1;
for (ll i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (ll i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(ll n, ll k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
mint p(ll n, ll k) {
return fact[n]*ifact[n-k];
}
} c(1000005);
int main(){
INT(N,M);
DisjointSet ds=DisjointSet(M+1);
V<ll> ans;
REP(i,N){
INT(k);
if(k==1){
INT(x);
x--;
if(!ds.same(x,M)){
ds.unite(x,M);
ans.push_back(i);
}
}
if(k==2){
INT(x,y);
x--; y--;
if(!ds.same(x,y)){
ds.unite(x,y);
ans.push_back(i);
}
}
}
mint p=2;
cout << p.pow(SZ(ans)) << " " << SZ(ans) << endl;
REP(i,SZ(ans)){
if(i) cout << " ";
cout << ans[i]+1;
}
cout << endl;
return 0;
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
class DSU {
public:
vector<int> uf, rank;
DSU(int n): uf(n), rank(n, 1) {iota(uf.begin(), uf.end(), 0);};
int find(int x){
if (uf[x] != x) uf[x] = find(uf[x]);
return uf[x];
}
bool unify(int x, int y){
int px = find(x), py = find(y);
bool ans = false;
if (px != py){
if (rank[px] > rank[py]) swap(px, py);
rank[py] += rank[px];
uf[px] = py;
ans = true;
}
return ans;
}
};
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
DSU dsu(m + 1);
vector<int> ans;
int x;
for (int i = 1; i <= n; i++) {
cin >> x;
int a = m, b = m;
cin >> a;
a--;
if (x == 2) {
cin >> b;
b--;
}
if (dsu.unify(a, b)) ans.push_back(i);
}
int t = 1;
for (int i = 0; i < ans.size(); i++) t = t * 2 % MOD;
cout << t << " " << ans.size() << endl;
for (int a: ans) cout << a << " ";
cout << endl;
return 0;
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef long double LD;
#define L first
#define smax(x, y) (x) = max((x), (y))
#define sz(x) ((int)(x).size())
#define R second
#define smin(x, y) (x) = min((x), (y))
#define PB push_back
#define MP make_pair
#define all(x) (x).begin(),(x).end()
const int maxn = 5e5 + 100;
const LL Mod = 1000 * 1000 * 1000 + 7;
int par[maxn];
bool mark[maxn];
int n, m;
vector<int> vec;
int root(int u) {
return par[u] < 0 ? u : par[u] = root(par[u]);
}
bool merge(int u) {
u = root(u);
if (mark[u])
return false;
return mark[u] = true;
}
bool merge(int u, int v) {
if ((u = root(u)) == (v = root(v)))
return false;
if (mark[u])
swap(u, v);
if (mark[u])
return false;
if (mark[v])
return mark[u] = true;
if (par[u] > par[v])
swap(u, v);
par[u] += par[v];
par[v] = u;
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
memset(par, -1, sizeof par);
cin >> n >> m;
LL ans = 1;
for (int i = 0; i < n; i++) {
int c;
int fi, se;
cin >> c >> fi;
if (c == 1) {
fi--;
if (merge(fi)) {
ans = ans * 2 % Mod;
vec.PB(i);
}
}
else {
cin >> se;
fi--, se--;
if (merge(fi, se)) {
ans = ans * 2 % Mod;
vec.PB(i);
}
}
}
cout << ans << ' ' << sz(vec) << '\n';
for (auto idx: vec)
cout << idx + 1 << ' ';
cout << '\n';
return 0;
}
| 12 |
CPP
|
#include "bits/stdc++.h"
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c> {i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni( != ) cerr << boolalpha << i; ris;
}
eni( == ) ris << range(begin(i), end(i));
}
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
#define int long long
const int mod = 1e9 + 7;
struct DSU {
vector<int> par;
DSU(int n) {
par.resize(n + 1);
for (int i = 1; i <= n; i++) {
par[i] = i;
}
}
int find(int a) {
if (a == par[a])
return a;
return par[a] = find(par[a]); //set parent to every node in the path to the root (path compression)
}
bool join(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return false;
par[a] = b;
return true;
}
};
int binexp(int a, int b) {
int ress = 1;
while (b > 0) {
if (b & 1) {
ress = ress * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return ress;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
DSU dsu(m + 1);
vector<int>ans;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
int v1;
cin >> v1;
int v2;
if (k == 2) {
cin >> v2;
}
else {
v2 = m + 1;
}
if (dsu.join(v1, v2)) {
ans.push_back(i + 1);
}
}
int t = binexp(2, ans.size());
cout << t << " " << ans.size() << endl;
for (auto it : ans) {
cout << it << " ";
}
cout << endl;
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
//int dirs[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
//int dirs[8][2]={{-1,0},{0,-1},{0,1},{1,0},{1,1},{-1,-1},{-1,1},{1,-1}};
long long modd=1e9+7;
vector<int> dsu;
vector<int> height;
vector<bool> has_loop;
int find_leader(int u)
{
if(dsu[u]==u) return u;
return dsu[u]=find_leader(dsu[u]);
}
void voeg_samen(int u, int v)
{
u=find_leader(u);
v=find_leader(v);
if(u==v) return;
if(height[u]>height[v])
{
dsu[v]=u;
if(has_loop[v]) has_loop[u]=true;
}
else if(height[u]<height[v])
{
dsu[u]=v;
if(has_loop[u]) has_loop[v]=true;
}
else
{
dsu[u]=v;
height[v]++;
if(has_loop[u]) has_loop[v]=true;
}
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("inp.in","r",stdin);
//freopen("outp.out","w",stdout);
int n, m;
scanf("%d %d", &n, &m);
dsu.assign(m,-1);
has_loop.assign(m,false);
height.assign(m,0);
for(int i=0; i<m; i++)
{
dsu[i]=i;
}
vector<int> ans_indices;
for(int i=0; i<n; i++)
{
int k;
scanf("%d", &k);
if(k==1)
{
int u;
scanf("%d", &u);
u--;
int leader=find_leader(u);
if(has_loop[leader])
{
continue;
}
else
{
has_loop[leader]=true;
ans_indices.push_back(i);
}
}
else
{
int u, v;
scanf("%d %d", &u, &v);
u--;
v--;
int ld1=find_leader(u);
int ld2=find_leader(v);
//cerr << u << " " << v << endl;
//cerr << ld1 << " " << ld2 << " " << has_loop[ld1] << " " << has_loop[ld2] << endl;
if((has_loop[ld1]&&has_loop[ld2])||ld1==ld2)
{
continue;
}
else
{
voeg_samen(ld1,ld2);
ans_indices.push_back(i);
}
}
/*for(int j=0; j<m; j++)
{
int ld=find_leader(j);
cerr << j << " has loop: " << has_loop[ld] << endl;
}
cerr << endl;*/
}
long long pow_two=1;
for(int i=0; i<ans_indices.size(); i++)
{
pow_two=(pow_two*2)%modd;
}
int a2=ans_indices.size();
printf("%I64d %d\n",pow_two,a2);
for(int i=0; i<ans_indices.size(); i++)
{
if(i != 0) printf(" ");
printf("%d",ans_indices[i]+1);
}
printf("\n");
return 0;
}
| 12 |
CPP
|
#include<bits/stdc++.h>
#define ll long long
#define FOR(i, s, e) for (int i = (s); i < (e); i++)
#define FOE(i, s, e) for (int i = (s); i <= (e); i++)
#define FOD(i, s, e) for (int i = (s); i >= (e); i--)
#define mp make_pair
using namespace std;
int n, m;
int p[500005], self[500005];
set<int> s;
int find(int x) { if (p[x] == x) return x; return p[x] = find(p[x]); }
int main () {
scanf("%d %d", &n, &m);
FOE(i, 1, m) p[i] = i, self[i] = 0;
FOR(i, 0, n) {
int k;
scanf("%d", &k);
if (k == 1) {
int x;
scanf("%d", &x);
x = find(x);
if (self[x] == 0) {
self[x] = 1;
s.emplace(i + 1);
}
} else {
int x, y;
scanf("%d%d", &x, &y);
x = find(x);
y = find(y);
if (x != y && (self[x] != 1 || self[y] != 1)) {
p[x] = y;
self[y] = max(self[x], self[y]);
s.emplace(i + 1);
}
}
}
long long res = 1;
for (int i = 0; i < (int)s.size(); i++)
res = (res * 2) % 1000000007ll;
printf("%lld %d\n", res, (int)s.size());
for (int i : s) printf("%d ", i);
puts("");
return 0;
}
| 12 |
CPP
|
#include <cstdio>
#include <iostream>
#define ll long long
using namespace std;
int n , m , cnt = 0;
const int N = 5e5 + 5;
const int mod = 1e9 + 7;
ll pow2[N];
int num[N] , fa[N];
int find(int x){
if(fa[x] == x)
return x;
return fa[x] = find(fa[x]);
}
int main(){
pow2[0] = 1;
cin >> n >> m;
for(int i = 1; i < N; i++)
pow2[i] = 1ll * 2 * pow2[i - 1] % mod;
for(int i = 1; i <= m + 1; i++)
fa[i] = i;
for(int i = 1; i <= n; i++){
int k;
int x , y;
scanf("%d" , &k);
if(k == 1){
scanf("%d" , &x);
y = m + 1;
}
else
scanf("%d%d" , &x , &y);
int fx , fy;
fx = find(x);
fy = find(y);
if(fx == fy)
continue;
fa[fx] = fy;
num[++cnt] = i;
}
ll ans = pow2[cnt] % mod;
cout << ans << " " << cnt << endl;
for(int i = 1; i <= cnt; i++)
printf("%d " , num[i]);
return 0;
}
| 12 |
CPP
|
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
from collections import deque
def find(parent,x):
if x == parent[x]:
return x
parent[x] = find(parent,parent[x])
return parent[x]
def union(parent,a,b,rank):
a,b = find(parent,a),find(parent,b)
if a != b:
if rank[a] < rank[b]:
a,b = b,a
parent[b] = a
if rank[a] == rank[b]:
rank[a] += 1
return 1
return 0
def main():
n,m = map(int,input().split())
ans,rank = [],[0]*m
parent,inde,path = [i for i in range(m)],[0]*m,[[] for _ in range(m)]
for i in range(1,n+1):
x = list(map(int,input().split()))
if x[0] == 1:
a = x[1]-1
if not inde[a]:
curr = deque([a])
while len(curr):
xx = curr.popleft()
inde[xx] = 1
for z in path[xx]:
if not inde[z]:
inde[z] = 1
curr.append(z)
path[xx] = []
ans.append(i)
else:
a,b = x[1]-1,x[2]-1
if (not inde[a] or not inde[b]) and union(parent,a,b,rank):
path[a].append(b)
path[b].append(a)
if inde[a] or inde[b]:
curr = deque([a])
while len(curr):
xx = curr.popleft()
inde[xx] = 1
for z in path[xx]:
if not inde[z]:
inde[z] = 1
curr.append(z)
path[xx] = []
ans.append(i)
print(pow(2,len(ans),10**9+7),len(ans))
print(*ans)
# Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
| 12 |
PYTHON3
|
#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define db(a) cout << fixed << #a << " = " << a << endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const ll MOD = 1e9 + 7;
const int MAXM = 5e5 + 5;
vector<int> parent(MAXM, -1);
vector<int> rank_(MAXM, -1);
vector<int> indices;
int get_parent(int p) {
if (parent[p] == p) {
return p;
}
parent[p] = get_parent(parent[p]);
return parent[p];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<ll> pow2(MAXM);
pow2[0] = 1;
for (int i = 1; i < MAXM; i++) {
pow2[i] = pow2[i - 1] * 2;
pow2[i] %= MOD;
}
vector<vector<int>> X(n, vector<int>());
for (int i = 0; i < n; i++) {
int k;
cin >> k;
if (k == 1) {
int x1;
cin >> x1;
X[i].push_back(x1);
if (parent[x1] == -1) {
parent[x1] = x1;
rank_[x1] = 1;
indices.push_back(i);
} else {
int p1 = get_parent(x1);
if (rank_[p1] == 1) continue;
parent[p1] = p1;
indices.push_back(i);
rank_[p1] = 1;
}
} else {
int x1, x2;
cin >> x1 >> x2;
X[i].push_back(x1);
X[i].push_back(x2);
if (parent[x1] == -1 && parent[x2] == -1) {
int p = x1;
parent[x1] = p;
parent[x2] = p;
indices.push_back(i);
rank_[p] = 2;
} else if (parent[x1] == -1) {
int p = get_parent(x2);
parent[x1] = p;
indices.push_back(i);
} else if (parent[x2] == -1) {
int p = get_parent(x1);
parent[x2] = p;
indices.push_back(i);
} else {
int p1 = get_parent(x1);
int p2 = get_parent(x2);
if (p1 == p2 || (rank_[p1] == 1 && rank_[p2] == 1)) continue;
indices.push_back(i);
parent[p2] = p1;
rank_[p1] = min(rank_[p1], rank_[p2]);
}
}
}
vector<set<int>> iP(m + 1, set<int>());
for (int i: indices) {
for (int x: X[i]) {
assert(parent[x] != -1);
iP[get_parent(x)].insert(x);
}
}
ll ans = 1;
for (int i = 0; i < m + 1; i++) {
if (iP[i].empty()) continue;
int sz = (int) iP[i].size();
assert(sz > 0);
if (rank_[i] == 2) {
ans *= pow2[sz - 1];
} else {
ans *= pow2[sz];
}
ans %= MOD;
}
cout << ans << " " << indices.size() << endl;
for (int i: indices) {
cout << i + 1 << " ";
}
cout << endl;
}
| 12 |
CPP
|
#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
int fa[500005];
int vis[500005];
int num[500005];
vector<int> tt;
long long ans=1;
int n,m;
int t;
struct nod{
int tp,a,b,id;
}arr[500005];
int cmp(nod a,nod b)
{
if (a.tp!=b.tp) return a.tp<b.tp;
return a.id<b.id;
}
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void work()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++) {fa[i]=i,vis[i]=0; num[i]=0;}
for (int i=1;i<=n;i++)
{
scanf("%d",&arr[i].tp);
arr[i].id=i;
if (arr[i].tp==1)
scanf("%d",&arr[i].a);
else
scanf("%d%d",&arr[i].a,&arr[i].b);
}
// sort(arr+1,arr+n+1,cmp);
for (int i=1;i<=n;i++)
{
if (arr[i].tp==1)
{
if (num[find(arr[i].a)]==0)
{
num[find(arr[i].a)]=1;
ans=ans+ans;
if (ans>=mod) ans-=mod;
tt.push_back(arr[i].id);
}
}
else
{
if (find(arr[i].a)!=find(arr[i].b))
{
if (num[find(arr[i].a)]==0 || num[find(arr[i].b)]==0)
{
num[find(arr[i].a)]+=num[find(arr[i].b)];
fa[find(arr[i].b)]=find(arr[i].a);
ans=ans+ans;
if (ans>=mod) ans-=mod;
tt.push_back(arr[i].id);
}
else
{
num[find(arr[i].a)]+=num[find(arr[i].b)];
fa[find(arr[i].b)]=find(arr[i].a);
}
}
}
}
printf("%lld %d\n",ans,(int)(tt.size()));
// sort(tt.begin(),tt.end());
int len=tt.size();
for (int i=0;i<len;i++)
printf("%d ",tt[i]);
printf("\n");
}
int main()
{
work();
}
| 12 |
CPP
|
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
// #include <atcoder/all>
// #include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
using namespace std;
// using namespace atcoder;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<p64,p64> pp64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 1e9+7;
double eps = 1e-12;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln '\n'
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define zero ll(0)
#define set_bits(x) __builtin_popcountll(x)
// #define mint modint998244353
ll mpow(ll a, ll b){
if(a==0) return 0;
if(b==0) return 1;
ll t1 = mpow(a,b/2);
t1 *= t1;
t1 %= MOD;
if(b%2) t1 *= a;
t1 %= MOD;
return t1;
}
ll mpow(ll a, ll b, ll p){
if(a==0) return 0;
if(b==0) return 1;
ll t1 = mpow(a,b/2,p);
t1 *= t1;
t1 %= p;
if(b%2) t1 *= a;
t1 %= p;
return t1;
}
ll modinverse(ll a, ll m){
ll m0 = m;
ll y = 0, x = 1;
if (m == 1) return 0;
while (a > 1){
ll q = a / m;
ll t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += m0;
return x;
}
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
ll range(ll l, ll r){
return l + mt()%(r-l+1);
}
ll rev(ll v){
return mpow(v,MOD-2);
}
ll nc2(ll n){
return (n*(n-1))/2;
}
// UnionFind U(n) :- n is number of elements
class UnionFind {
v64 p, rank, sz;
public:
UnionFind(ll N) {
rank.assign(N, 0);
p.assign(N, 0);
sz.assign(N, 1);
forn(i,N) p[i] = i;
}
void inc(){
rank.pb(0);
p.pb(sz(p));
sz.pb(1);
}
ll findSet(ll i) { // returns the head of group containing element i
return (p[i] == i) ? i : (p[i] = findSet(p[i]));
}
bool isSameSet(ll i, ll j) { // checks whether i and j are in same group
return findSet(i) == findSet(j);
}
void unionSet(ll i, ll j) { // unify group containing i and j(if already in same set then do nothing)
if (!isSameSet(i, j)) {
ll x = findSet(i), y = findSet(j);
if (rank[x] > rank[y]) {
p[y] = x;
sz[x] += sz[y];
}
else {
p[x] = y;
if (rank[x] == rank[y]) rank[y]++;
sz[y] += sz[x];
}
}
}
ll sz1(ll i){ // returns the sz[i] (to check the size of group containing i use sz1(findSet(i)))
return sz[i];
}
};
void solve(){
ll n,m;
cin >> n >> m;
ll x[n][2];
UnionFind U(m+1);
v64 ans;
forn(i,n){
ll k;
cin >> k;
forn(j,k) cin >> x[i][j];
if(k==1){
if(!U.isSameSet(x[i][0],0)){
U.unionSet(0,x[i][0]);
ans.pb(i+1);
}
}
else{
if(!U.isSameSet(x[i][0],x[i][1])){
U.unionSet(x[i][0],x[i][1]);
ans.pb(i+1);
}
}
}
ll num = 1;
forn(i,sz(ans)){
num*=2;
num %= MOD;
}
cout << num << " " << sz(ans) << ln;
for(auto it : ans) cout << it << " ";
cout << ln;
}
int main()
{
fast_cin();
ll t=1;
// cin >> t;
forn(i,t) {
// cout << "Case #" << i+1 << ": ";
solve();
}
return 0;
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+2,p=1e9+7;
int f[N],st[N],ed[N];
int n,c,i,t,m,j,k,tp,x,y,ans,z,w,la,s;
inline int ksm(register int x,register int y)
{
register int r=1;
while (y)
{
if (y&1) r=(ll)r*x%p;
x=(ll)x*x%p;
y>>=1;
}
return r;
}
inline void read(register int &x)
{
register int c=getchar(),fh=1;
while ((c<48)||(c>57))
{
if (c=='-') {c=getchar();fh=-1;break;}
c=getchar();
}
x=c^48;c=getchar();
while ((c>=48)&&(c<=57))
{
x=x*10+(c^48);
c=getchar();
}
x*=fh;
}
int getf(int x) {return f[x]==x?x:f[x]=getf(f[x]);}
int main()
{
read(m);read(n);
for (i=1;i<=n;i++) f[i]=i;
for (i=1;i<=m;i++)
{
read(z);
if (z==1) {read(x);x=getf(x);if (!ed[x]) ed[x]=1,st[++tp]=i;continue;}
read(x);read(y);
x=getf(x);y=getf(y);
if (x==y||ed[x]&&ed[y]) continue;
st[++tp]=i;f[x]=y;ed[y]|=ed[x];
}
printf("%d %d\n",ksm(2,tp),tp);
for (i=1;i<=tp;i++) printf("%d%c",st[i],i==tp?10:32);
}
| 12 |
CPP
|
#include <iostream>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <assert.h>
#include <queue>
typedef long long ll;
const int mod=1e9+7;
using namespace std;
int n,m,k,v,ans=1,x,y;
int a[500005],par[500005];
vector<int>res;
int find(int x){
return par[x]==x?x:par[x]=find(par[x]);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i)
{
par[i]=i;
}
for(int i=1;i<=n;++i){
scanf("%d",&k);
a[1]=a[2]=m+1;
for(int j=1;j<=k;++j){
scanf("%d",&a[j]);
}
if((x=find(a[1]))!=(y=find(a[2]))){
par[y]=x;
ans=2ll*ans%mod;
res.push_back(i);
}
}
printf("%d %d\n",ans,(int)res.size());
for(auto &x:res){
printf("%d ",x);
}
return 0;
}
| 12 |
CPP
|
#include<bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x...) do { cout << "\033[32;1m " << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template<template<typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) { for (auto v: a) cout << v << ' '; err(x...); }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;
template<class T> void mkuni(vector<T>&v)
{
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
}
ll rand_int(ll l, ll r) //[l, r]
{
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
return uniform_int_distribution<ll>(l, r)(gen);
}
template<class T>
void print(T x,int suc=1)
{
cout<<x;
if(suc==1) cout<<'\n';
else cout<<' ';
}
template<class T>
void print(const vector<T>&v,int suc=1)
{
for(int i=0;i<v.size();i++)
print(v[i],i==(int)(v.size())-1?suc:2);
}
const int maxn = 5e5 + 5;
const int mod = 1e9 + 7;
int n, m, fa[maxn], x, y, t, vis[maxn];
pair<int, int> p[maxn];
vector<int> ans;
int fnd(int x)
{
if (fa[x] == x) return x;
return fa[x] = fnd(fa[x]);
}
void init()
{
for (int i = 0; i <= m; i++)
fa[i] = i;
}
ll qpow(ll a, ll b)
{
ll ans = 1;
while (b) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
init();
int cnt = 0;
for (int i = 0; i < n; i++) {
cin >> t;
if (t == 2) {
cin >> x >> y;
int fx = fnd(x), fy = fnd(y);
if (fx == fy) continue;
if (vis[fx] && vis[fy])
fa[fx] = fy;
else
fa[fx] = fy, vis[fy] |= vis[fx] , ans.push_back(i+1);
}
else {
cin >> x;
if (!vis[fnd(x)]) vis[fnd(x)] = 1, ans.push_back(i+1);
}
//cout << ans.size() << '\n';
}
cout << qpow(2, ans.size()) << ' ' << ans.size() <<'\n';
for (auto &x : ans)
cout << x << ' ';
cout << '\n';
}
| 12 |
CPP
|
from collections import defaultdict,deque
import sys
import bisect
import math
input=sys.stdin.readline
mod=1000000007
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
def find(self, a):
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a:
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b):
self.parent[self.find(b)] = self.find(a)
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
UF = UnionFind(m + 1)
MOD = 10 ** 9 + 7
out = []
for i in range(1, n + 1):
l = list(map(int, input().split()))
if len(l) == 2:
u = 0
v = l[1]
else:
#if vector contains two cordinates equal to one, then it means they are
#connected, that s we cannot count one without counting the other
_, u, v = l
uu = UF.find(u)
vv = UF.find(v)
#if any edge forms a cycle then discard that edge, as it's function is redundant
if uu != vv:
UF.union(uu,vv)
out.append(i)
print(pow(2, len(out), MOD), len(out))
print(' '.join(map(str,out)))
| 12 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
#define send {ios_base::sync_with_stdio(false);}
#define help {cin.tie(NULL);}
#define f first
#define s second
#define pb push_back
#define vi vector<long long>
#define vii vector<pair<long long,long long>>
#define dict unordered_map<long long, long long>
#define contains(d,x) (d.find(x)!=d.end())
typedef long long ll;
typedef long double lld;
typedef unsigned long long ull;
const lld pi = 3.14159265358979323846;
const ll mod = 1000000007;
// const ll mod = 998244353;
ll n, m, k, l, p, q, r, x, y, z, sz;
const ll template_array_size = 1e6 + 5;
ll a[template_array_size];
ll b[template_array_size];
ll c[template_array_size];
string s, t;
ll ans = 0;
ll ts[5000000];
ll power(int x, int y){
if(!y) return 1;
ll temp = power(x, y/2);
temp = (temp*temp)%mod;
if(y&1){
return (temp*x)%mod;
}
return temp;
}
int find(int i, ll rep[]) {
int t = i;
while(rep[i]!=i){
i = rep[i];
}
while(rep[t]!=t){
t = rep[t];
rep[t] = i;
}
return i;
}
int onion(int i, int j, ll rep[]){
i = find(i,rep);
j = find(j,rep);
rep[j] = i;
return i!=j;
}
void solve(int tc = 0) {
cin >> n >> m;
vector<vii> adj(m+1);
vi chosen;
for(int i=0; i<m+1; i++) a[i] = i;
for(int i=0; i<n; i++) {
cin >> x;
if (x==1) {
cin >> y;
x = 0;
} else {
cin >> x >> y;
}
if(onion(x,y,a)){
chosen.pb(i+1);
}
}
p = chosen.size();
ans = power(2,p);
cout << ans << " " << p << endl;
for(int i : chosen) cout << i <<" ";
cout << endl;
}
int main() {
send help
// int tc = 1;
// cin >> tc;
// for (int t = 0; t < tc; t++) solve(t);
solve();
}
| 12 |
CPP
|
/**
* Author : Ujjawal Pabreja [cuber_coder]
* Email : [email protected]
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
void Solve() {
int n, m;
cin >> n >> m;
vector<int> vis(m);
vector<int> parent(m);
iota(parent.begin(), parent.end(), 0);
function<int(int)> Find = [&](int u) {
if (u != parent[u]) {
return parent[u] = Find(parent[u]);
}
return parent[u];
};
function<bool(int, int)> Unite = [&](int u, int v) {
u = Find(u);
v = Find(v);
if (u != v and vis[u] + vis[v] != 2) {
parent[v] = u;
vis[u] += vis[v];
return true;
}
return false;
};
int ans = 1, mod = 1e9 + 7;
vector<int> subset;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
int u, v;
if (k == 1) {
cin >> u;
u--;
u = Find(u);
if (vis[u] == 0) {
vis[u] = 1;
subset.push_back(i + 1);
ans = (ans * 2) % mod;
}
} else {
cin >> u >> v;
u--, v--;
if (Unite(u, v)) {
subset.push_back(i + 1);
ans = (ans * 2) % mod;
}
}
}
cout << ans << " " << subset.size() << endl;
for (int i = 0; i < subset.size(); i++) {
cout << subset[i] << " \n"[i == subset.size() - 1];
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int testcases = 1;
// cin >> testcases;
for (int i = 1; i <= testcases; i++) {
Solve();
}
return 0;
}
| 12 |
CPP
|
#include <bits/stdc++.h>
#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define per(i, r, l) for (register int i = r; i >= l; i--)
#define srep(i, l, r) for (register int i = l; i < r; i++)
#define sper(i, r, l) for (register int i = r; i > l; i--)
#define erep(i, x) for (register int i = h[x]; i; i = e[i].next)
#define erep2(i, x) for (register int& i = cur[x]; i; i = e[i].next)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define fi first
#define se second
#define ui unsigned int
#define ld long double
#define pb push_back
#define pc putchar
#define lowbit(x) (x & -x)
#define maxr 2000020
#define gc() ((p1 == p2 && (p2 = (p1 = buffer) + fread(buffer, 1, maxr, stdin), p1 == p2)) ? EOF : *p1++)
using namespace std;
namespace Fast_Read{
char buffer[maxr], *p1, *p2;
template<class T> void read_signed(T& x){
char ch = gc(); x = 0; bool f = 1;
while (!isdigit(ch) && ch != '-') ch = gc();
if (ch == '-') f = 0, ch = gc();
while ('0' <= ch && ch <= '9') x = (x << 1) + (x << 3) + ch - '0', ch = gc();
x = (f) ? x : -x;
}
template<class T, class... Args> void read_signed(T& x, Args&... args){
read_signed(x), read_signed(args...);
}
template<class T> void read_unsigned(T& x){
char ch = gc(); x = 0;
while (!isdigit(ch)) ch = gc();
while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = gc();
}
template<class T, class... Args> void read_unsigned(T& x, Args&... args){
read_unsigned(x), read_unsigned(args...);
}
#define isletter(ch) ('a' <= ch && ch <= 'z')
int read_string(char* s){
char ch = gc(); int l = 0;
while (!isletter(ch)) ch = gc();
while (isletter(ch)) s[l++] = ch, ch = gc();
s[l] = '\0'; return l;
}
}using namespace Fast_Read;
int _num[20];
template <class T> void write(T x, char sep = '\n'){
if (!x) {putchar('0'), putchar(sep); return;}
if (x < 0) putchar('-'), x = -x;
int c = 0;
while (x) _num[++c] = x % 10, x /= 10;
while (c) putchar('0' + _num[c--]);
putchar(sep);
}
#define read read_unsigned
#define reads read_string
#define writes puts
#define maxn 600020
#define maxm
#define maxs
#define maxb
#define inf
#define eps
#define M 1000000007
#define ll long long int
int ufs[maxn], siz[maxn]; bool loop[maxn];
int find_root(int x){
return ufs[x] = (ufs[x] == x) ? x : find_root(ufs[x]);
}
bool merge(int x, int y){
x = find_root(x), y = find_root(y);
if (x == y || (loop[x] && loop[y])) return false;
ufs[x] = y;
loop[y] = loop[x] | loop[y];
siz[y] += siz[x];
return true;
}
int ans[maxn], cs = 0, n, m;
int main(){
int x, y, k;
read(n, m);
rep(i, 1, m) ufs[i] = i, siz[i] = 1, loop[i] = 0;
rep(i, 1, n) {
read(k);
if (k == 1) {
read(x);
x = find_root(x);
if (!loop[x]) loop[x] = 1, ans[++cs] = i;
}
else {
read(x, y);
if (merge(x, y)) ans[++cs] = i;
}
}
int res = 0;
rep(i, 1, m) {
if (ufs[i] == i) {
res = res + siz[i] + (loop[i] ? 0 : -1);
}
}
int out = 1;
rep(i, 1, res) out = out << 1, out %= M;
write(out, ' '), write(cs);
rep(i, 1, cs) write(ans[i], ' ');
return 0;
}
| 12 |
CPP
|
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long int lld;
typedef long double llf;
typedef pair<int, int> pii;
const lld M = 1e9 + 7;
const int MAXN = 500002;
int n, m;
vector<int> adj[MAXN];
int padre[MAXN];
int raiz(int nodo) {
if (padre[nodo] == -1) return nodo;
return padre[nodo] = raiz(padre[nodo]);
}
int pot2(int e) {
if (e == 0) return 1;
lld x = pot2(e / 2);
x *= x;
x %= M;
if (e & 1) x *= 2;
return x % M;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> m >> n;
fill(padre, padre + n + 1, -1);
vector<int> li;
for (int i = 0; i < m; ++i) {
int k;
cin >> k;
int a, b;
cin >> a;
if (k == 1) b = 0;
else cin >> b;
// Para ver si es l.i.
// aplicamos MST
int ra = raiz(a);
int rb = raiz(b);
if (ra == rb) continue; // el vector es l.d.
padre[ra] = rb;
li.push_back(i+1);
}
cout << pot2(li.size()) << " " << li.size() << "\n";
for (int x : li)
cout << x << " ";
cout << "\n";
return 0;
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
const int MAXN = 5e5 + 13;
const int INF = 1e9 + 7;
int N, M;
int dsu[MAXN];
vector<pair<int, pii> > edges;
vi ans;
int prod = 1;
int get(int u)
{
return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
bool merge(int u, int v)
{
u = get(u);
v = get(v);
if (u == v) return false;
dsu[u] = v;
return true;
}
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> M >> N;
iota(dsu, dsu + N + 1, 0);
FOR(i, 0, M)
{
int c;
cin >> c;
if (c == 1)
{
int x;
cin >> x;
edges.PB({i, {0, x}});
}
else
{
int x, y;
cin >> x >> y;
edges.PB({i, {x, y}});
}
}
for (auto e : edges)
{
int u = e.se.fi, v = e.se.se;
if (merge(u, v))
{
ans.PB(e.fi);
prod += prod; if (prod >= INF) prod -= INF;
}
}
cout << prod << ' ' << SZ(ans) << '\n';
FOR(i, 0, SZ(ans))
{
cout << ans[i] + 1 << " \n"[i == SZ(ans) - 1];
}
return 0;
}
| 12 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
#define INF 10000000000000LL
typedef long long int ll;
typedef pair<ll,ll> ii;
typedef pair<ii,ll> iii;
typedef pair<ii,ii> ii2;
typedef vector<ll> vi;
typedef vector<ii> vii;
#define getbit(n,i) (((n)&(1LL<<(i)))!=0)
#define setbit0(n,i) ((n)&(~(1LL<<(i))))
#define setbit1(n,i) ((n)|(1LL<<(i)))
#define lastone(n) ((n)&(-(n)))
#define read freopen("debug//in.txt","r",stdin)
#define write freopen("debug//out.txt","w",stdout)
#define DBG(a) cerr<<#a<<" ->->->-> "<<a<<"\n"
#define fi first
#define se second
#define PI (acos(-1))
#define fastread ios::sync_with_stdio(false);cin.tie(NULL)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mod (1000000007)
#define asz 500005
template<class T,class V> ostream& operator<<(ostream &s,pair<T,V> a)
{
s<<a.fi<<' '<<a.se;
return s;
}
int p[asz];
int fnd(int i){
if(p[i] == i)return i;
return p[i] = fnd(p[i]);
}
int main()
{
fastread;
#ifdef FILE_IO2
read;
write;
#endif
int T=1;
// cin>>T;
for(int qq=1;qq<=T; qq++){
int n,m;
cin>>n>>m;
for(int i=1;i<=m+1;i++)p[i] = i;
vii v(n);
for(int i=0;i<n;i++){
int k;
cin>>k;
cin>>v[i].fi;
if(k == 2)cin>>v[i].se;
else v[i].se = m+1;
if(v[i].fi>v[i].se)swap(v[i].fi,v[i].se);
}
vi ans;
ll pw = 1;
for(int i=0; i<n; i++){
int u = fnd(v[i].fi);
int u2 = fnd(v[i].se);
if(u!=u2){
p[u2] = u;
ans.push_back(i+1);
pw = pw*2%mod;
}
}
cout<<pw<<' '<<ans.size()<<endl;
for(auto x:ans)cout<<x<<' ';
cout<<endl;
}
}
| 12 |
CPP
|
#include <bits/stdc++.h>
//#include <bits/extc++.h>
#define ll long long
#define ull unsigned ll
#define endl "\n"
#define pb push_back
#define ms(v,x) memset(v,x,sizeof(v))
#define ff first
#define ss second
#define td(v) v.begin(),v.end()
#define rep(i,a,n) for (int i=(a);i<(n);i++)
#define per(i,a,n) for (int i=(n-1);i>=a;i--)
#define trav(a, x) for(auto& a : x)
#define re(v) {for(auto &_re : v) cin >> _re;}
#define pr(v) {for(auto _pr : v) cout << _pr << " "; cout << endl;}
//#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define eb emplace_back
using namespace std;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll M = 1e9 + 7;
//const ll M = 998244353;
//const ll M = 1e9 + 9;
//const ll M = 1e6;
#define tiii tuple<int,int,int>
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll binpow(ll a, ll b){
ll ret = 1;
while(b){
if(b & 1){
ret = ret * a % M;
}
a = a * a % M;
b >>= 1;
}
return ret;
}
void solve(){
int n, m;
cin >> n >> m;
vi basis(m + 1);
vi nxt(m + 1);
iota(td(nxt), 0);
function<int (int)> f = [&](int i){
if(nxt[i] == i or nxt[i] == -1) return nxt[i];
return nxt[i] = f(nxt[i]);
};
vector<vector<int>> vecs(n);
vector<int> s;
for(int i=0;i<n;i++){
int k; cin >> k;
vecs[i].resize(k);
re(vecs[i]);
sort(td(vecs[i]));
// check if is represented
int a = f(vecs[i][0]);
int b = -1;
if(k > 1) b = f(vecs[i][1]);
if(a == b){
continue;
}
if(a == -1){
if(b == -1) continue;
basis[b] = 1;
s.eb(i);
nxt[b] = -1;
}
else{
basis[a] = i;
s.eb(i);
nxt[a] = b;
}
}
cout << binpow(2, s.size()) << " " << s.size() << endl;
for(int x : s) cout << x + 1 << " ";
cout << endl;
}
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
int t = 1;
//cin >> t;
while(t--){
solve();
}
}
| 12 |
CPP
|
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<random>
#include<ctime>
#include<vector>
#include<cmath>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+5,N2=20;
const ll INF=1e18+5;
inline ll read()
{
ll sum=0,fh=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')fh=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
sum=sum*10+c-'0';
c=getchar();
}
return sum*fh;
}
inline int read2(int *a)
{
int x=0;
char c=getchar();
while(c<'a'||c>'z')c=getchar();
while(c>='a'&&c<='z')
{
a[++x]=c;
c=getchar();
}
return x;
}
inline void write(ll x)
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
}
inline int gcd(int x,int y)
{
return y==0?x:gcd(y,x%y);
}
inline int ab(int x)
{
return x<0?-x:x;
}
#define pi pair<int,int>
int fa[N];
inline int fifa(int x)
{
return x==fa[x]?x:fa[x]=fifa(fa[x]);
}
int a[N]={0};
int ans[N],js=0;
pi c[N];
int main()
{
//freopen("qwq.txt","r",stdin);
int n=read(),m=read();
for(int i=1;i<=m+1;++i)fa[i]=i;
for(int i=1;i<=n;++i)
{
int k=read();
int x=read(),y=(k==2?read():m+1);
if(x>y)swap(x,y);
if(!a[x])
{
a[x]=i;
ans[++js]=i;
fa[x]=y;
}
else
{
int x2=fifa(c[a[x]].first);
if(x2!=y)
{
x=x2;
if(x>y)swap(x,y);
if(!a[x])
{
a[x]=i;
ans[++js]=i;
fa[x]=y;
}
else
{
int x2=fifa(c[a[x]].first);
if(x2!=y)
{
x=x2;
if(x>y)swap(x,y);
if(x!=m+1&&!a[x])
{
a[x]=i;
ans[++js]=i;
fa[x]=y;
}
}
}
}
}
c[i]=pi(x,y);
}
int sum=1,p=1e9+7;
for(int i=1;i<=js;++i)
{
sum=(sum*2)%p;
}
cout<<sum<<" "<<js<<endl;
for(int i=1;i<=js;++i)
{
write(ans[i]),putchar(' ');
}
}
| 12 |
CPP
|
// codeforce
// wangqc
#include<iostream>
#include<vector>
using namespace std;
const int M = 1e9+7;
vector<int> p, rk, ans;
int find(int x)
{
return x == p[x] ? x : p[x]=find(p[x]);
}
int connect(int x, int y)
{
int px = find(x), py = find(y);
if(px == py) return false;
if(rk[px] < rk[py]) swap(px, py);
if(rk[px] == rk[py]) rk[px]++;
p[py] = px;
return true;
}
int main()
{
int n, m;
cin >> n >> m;
p.resize(m+2), rk.resize(m+2), ans.clear();
for(int i = 0; i <= m; i++)
p[i] = i, rk[i] = 0;
int k, x, y;
for(int i = 1; i <= n; i++)
{
cin >> k >> x;
if(k == 1) y = m+1;
else cin >> y;
if(connect(x, y)) ans.push_back(i);
}
n = ans.size(), m = 1;
for(int _ = 0; _ < n; _++) m = (m<<1) % M;
cout << m << " " << n << endl;
for(int i : ans) cout << i << " ";
cout << endl;
return 0;
}
| 12 |
CPP
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.