# CF1519E Off by One

## 二、解法

#include <cstdio>
#include <vector>
#include <iostream>
#include <map>
using namespace std;
const int M = 400005;
#define make make_pair
#define ll long long
#define pll pair<ll,ll>
{
int x=0,f=1;char c;
while((c=getchar())<'0' || c>'9') {if(c=='-') f=-1;}
while(c>='0' && c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
return x*f;
}
int n,m,res,d[M];vector<int> g[M],h[M],ans[M];
map<pll,int> mp;
ll gcd(ll a,ll b)
{
return !b?a:gcd(b,a%b);
}
pll get(ll a,ll b,ll c,ll d)
{
ll g=gcd(a*d,c*b);
return make(a*d/g,c*b/g);
}
void dfs(int u,int fa)
{
d[u]=d[fa]+1;int tmp=-1;
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(d[v])
{
if(d[v]>d[u]) continue;
if(v==fa && tmp==-1) tmp=i;
else ans[v].push_back(h[u][i]);
}
else dfs(v,u);
}
if(fa)
{
if(ans[u].size()&1) ans[u].push_back(h[u][tmp]);
else ans[fa].push_back(h[u][tmp]);
}
}
signed main()
{
for(int i=1;i<=n;i++)
{
int &x=mp[get(a+b,b,c,d)],&y=mp[get(a,b,c+d,d)];
if(!x) x=++m;if(!y) y=++m;
g[x].push_back(y);g[y].push_back(x);
h[x].push_back(i);h[y].push_back(i);
}
for(int i=1;i<=m;i++)
if(!d[i]) dfs(i,0);
for(int i=1;i<=m;i++)
res+=ans[i].size()/2;
printf("%d\n",res);
for(int i=1;i<=m;i++)
for(int j=1;j<ans[i].size();j+=2)
printf("%d %d\n",ans[i][j-1],ans[i][j]);
}

posted @ 2021-05-03 12:27  C202044zxy  阅读(65)  评论(0编辑  收藏  举报