Sgu213-strong Defence

Source: Internet
Author: User

Main topic:

Give you an s,t diagram, and then a one-stop representation of the beginning and end, and then enter the N,m,s,t, the M-bar without a phase into a set of L, so that any one of the set of the edge is removed, can not reach s (or S to T, specifically do not remember, but almost ...). ), and then you want to find the largest l, and then output each edge set.


Problem Solving Ideas:

First see to make unreachable, I think of the network flow, but later thought to find, actually very simple.

First run the shortest path with S as the starting point, and then get a dis[t] that represents the shortest distance from S to T, and then l=dis[t]

Next is to group the edges, for i=1~dis[t], Edge <u,v> (we assume dis[u]<=dis[v]) if satisfies dis[u]+1=dis[v], then the edge is added to the Dis[v] collection, The rest of the unsatisfied side just put on the just fine.

The correctness of the specific algorithm is not given proof.


AC Code:

#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm > #include <iostream> #include <vector> #define MAX (a) (a) > (b)? A):(B) #define MIN (a) > (b) ( b):(a)) using namespace Std;int n,m,s,t;struct bian_{int num;int Next;} Bian[160010]={{0,0}};int g[160010][2]={{0}};int first[410]={0};int dis[410]={0};int hash[410]={0};int dui[1000010]= {0};inline void Add (int p,int q,int k) {bian[k].num=q;bian[k].next=first[p]; First[p]=k;return;} void Spfa () {int duip=1;memset (dis,0x3f3f3f3f,sizeof (dis));d is[s]=0;dui[duip]=s;hash[s]=1;for (int i=1;i<=duip;i + +) {for (int p=first[dui[i]];p!=0;p=bian[p].next) {if (Dis[dui[i]]+1<dis[bian[p].num] && hash[bian[p].num ]==0) {hash[bian[p].num]=1;dui[++duip]=bian[p].num;dis[bian[p].num]=dis[dui[i]]+1;}} hash[dui[i]]=0;} return;} int main () {scanf ("%d%d%d%d", &n,&m,&s,&t), for (int i=1;i<=m;i++) {int p,q;scanf ("%d%d", &p, &AMP;Q); ADD (p,q,i*2-1); ADD (q,p,i*2); g[i][0]=P;g[i][1]=q;} SPFA (); vector <int> ans[410];for (int i=1;i<=m;i++) {int p=dis[g[i][0]],q=dis[g[i][1]];if (P&GT;Q) swap (P,Q); if (q==p+1) {if (q>=dis[t]) ans[dis[t]].push_back (i); else ans[q].push_back (i);} else Ans[dis[t]].push_back (i);} printf ("%d\n", Dis[t]), for (int i=1;i<=dis[t];i++) {printf ("%d", Ans[i].size ()), and for (int j=ans[i].size () -1;j>=0 ; j--) printf ("%d%c", ans[i][j],j==0? \ n ': ');} return 0;}


Sgu213-strong Defence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.