@KirinBill
2017-10-28T06:57:52.000000Z
字数 4630
阅读 1206
题解
套题
目录
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
#include <vector>
using std::vector;
const int MAXN=100005,MAXM=200005;
int n,cnt;
int he[MAXN],deg[MAXN],de[MAXN];
bool vis[MAXN];
vector<int> ans[MAXN];
struct line{int to,nex;}ed[MAXM<<1];
inline void addE(int u,int v){
static int cnt;
ed[++cnt]=(line){v,he[u]};
he[u]=cnt;
}
void DFS(int u,int fa){
vis[u]=true;
for(int i=he[u],v;i;i=ed[i].nex){
v=ed[i].to;
if(v==fa) continue;
if(!vis[v]){
de[v]=de[u]+1;
DFS(v,u);
}
else if(de[v]>de[u]){
++deg[u];
ans[u].push_back(v);
}
}
if(fa){
if(deg[u]&1) ans[u].push_back(fa);
else{
++deg[fa];
ans[fa].push_back(u);
}
}
}
inline void solve(){
for(int i=1;i<=n;++i){
if(!vis[i]) DFS(i,0);
}
for(int i=1;i<=n;++i)
cnt+=ans[i].size()>>1;
}
int main(){
setIO("graph");
int m;
read(n),read(m);
for(int i=1,u,v;i<=m;++i){
read(u),read(v);
addE(u,v),addE(v,u);
}
solve();
write(cnt,'\n');
for(int i=1;i<=n;++i){
for(int j=0;j+1<ans[i].size();j+=2){
write(ans[i][j],' ');
write(i,' ');
write(ans[i][j+1],'\n');
}
}
return 0;
}
priority_queue
即可,我也不知道为啥。。。
#include <cstdio>
#include <algorithm>
#include <climits>
#include <queue>
#include <cstring>
using std::min;
using std::priority_queue;
const int MAXN=500005;
int n,k;
int p[MAXN],id[MAXN];
int he[MAXN],inD[MAXN];
struct line{int to,nex;}ed[MAXN<<1];
class segT{
#define ls (u<<1)
#define rs (ls|1)
private:
int minw[MAXN<<2];
void upd(int u){
minw[u]=min(minw[ls],minw[rs]);
}
void ist(int u,int l,int r,int pos,int x){
if(l==r){
minw[u]=min(minw[u],x);
return;
}
int mid=l+r>>1;
if(pos<=mid) ist(ls,l,mid,pos,x);
else ist(rs,mid+1,r,pos,x);
upd(u);
}
int qry(int u,int l,int r,int lp,int rp){
if(lp<=l && r<=rp) return minw[u];
int mid=l+r>>1,ret=INT_MAX;
if(lp<=mid) ret=qry(ls,l,mid,lp,rp);
if(rp>mid) ret=min(ret,qry(rs,mid+1,r,lp,rp));
return ret;
}
public:
segT(){memset(minw,0x7f,sizeof(minw));}
void ist(int pos,int x){ist(1,1,n,pos,x);}
int qry(int lp,int rp){return qry(1,1,n,lp,rp);}
#undef ls
#undef rs
}T;
inline void addE(int u,int v){
static int cnt;
ed[++cnt]=(line){v,he[u]};
he[u]=cnt;
}
inline void build(){
for(int i=n,v;i;--i){
v=T.qry(id[i]-k+1,id[i]);
if(v<=n){
addE(id[v],id[i]);
++inD[id[i]];
}
v=T.qry(id[i],id[i]+k-1);
if(v<=n){
addE(id[v],id[i]);
++inD[id[i]];
}
T.ist(id[i],i);
}
}
inline void topo_sort(){
static priority_queue<int> pq;
for(int i=1;i<=n;++i){
if(!inD[i]) pq.push(i);
}
for(int i=n,u;i;--i){
u=id[i]=pq.top();
pq.pop();
for(int j=he[u],v;j;j=ed[j].nex){
v=ed[j].to;
if(--inD[v]==0) pq.push(v);
}
}
}
int main(){
freopen("permutation.in","r",stdin);
freopen("permutation.out","w",stdout);
scanf("%d%d",&n,&k);
for(int i=1;i<=n;++i)
scanf("%d",&p[i]);
for(int i=1;i<=n;++i)
id[p[i]]=i;
build();
topo_sort();
for(int i=1;i<=n;++i)
p[id[i]]=i;
for(int i=1;i<=n;++i)
printf("%d\n",p[i]);
return 0;
}
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
int main(){
setIO("tree");
int n;
read(n);
long long ans=0;
for(int i=1,w;i<n;++i){
scanf("%*d%*d%d",&w);
ans+=w;
}
write(ans);
return 0;
}